Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Advanced HTTP Methods and Status Codes

1. HTTP Methods

HTTP defines a set of request methods that indicate the desired action to be performed on the identified resource. Here are some advanced HTTP methods you should know:

  • PATCH: Used to apply partial modifications to a resource.
  • OPTIONS: Describes the communication options for the target resource.
  • TRACE: Performs a message loop-back test along the path to the target resource.
  • CONNECT: Establishes a tunnel to the server identified by the target resource.
  • Note: While PATCH is commonly used for partial updates, it should only be used with RESTful APIs that support it.

    Example of PATCH Request

    PATCH /users/123
    Content-Type: application/json
    
    {
        "name": "New Name"
    }

    2. HTTP Status Codes

    HTTP status codes are issued by a server in response to a client's request made to the server. Here are some advanced status codes:

  • 206 Partial Content: The server is delivering only part of the resource due to a range header sent by the client.
  • 308 Permanent Redirect: The request and all future requests should be repeated using another URI.
  • 451 Unavailable For Legal Reasons: The server is denying access to the resource in accordance with legal requirements.
  • 429 Too Many Requests: The user has sent too many requests in a given amount of time.
  • Tip: Always check the status code when making API calls to handle responses appropriately.

    Example of a 206 Response

    HTTP/1.1 206 Partial Content
    Content-Range: bytes 0-499/1234
    Content-Length: 500
    

    3. Best Practices

    Here are some best practices to follow when using advanced HTTP methods and status codes:

  • Always use the correct HTTP method according to the RESTful principles.
  • Return appropriate status codes in your API responses to inform clients about the result of their requests.
  • Document your API endpoints clearly, including the expected HTTP methods and status codes.
  • Implement rate limiting to handle 429 status codes effectively.
  • 4. FAQ

    What is the difference between PUT and PATCH?

    PUT replaces the entire resource, while PATCH applies partial modifications to the resource.

    When should I use the OPTIONS method?

    Use the OPTIONS method to find out the communication options available for a given resource.

    What does the 308 status code signify?

    The 308 status code indicates that the resource has been permanently moved to a new URI, and all future requests should use that URI.