HTTP Methods Cheat Sheet

Introduction

HTTP methods define how a web client (like a browser or an API consumer) interacts with resources on a server. Each method has a unique purpose, set of semantics, and characteristics. Understanding these methods is essential for designing efficient, RESTful APIs and ensuring proper client-server communication.

This cheat sheet outlines all the standard HTTP methods, their use cases, and their properties to help you work effectively with web APIs.


HTTP Methods Cheat Sheet

Here’s a comprehensive overview of the HTTP methods:

Method Description Idempotent Safe Cacheable
GET Retrieves data from the server. Yes Yes Yes
POST Submits data to be processed on the server, often resulting in a state change. No No No
PUT Updates or replaces a resource entirely. Yes No No
DELETE Deletes a specified resource. Yes No No
HEAD Same as GET but retrieves only headers (no body). Used to check resource status. Yes Yes Yes
OPTIONS Describes communication options available for the target resource. Yes Yes No
PATCH Partially updates a resource. No No No
CONNECT Establishes a network connection tunnel, typically for SSL encryption. No No No
TRACE Performs a message loop-back test for debugging. Yes Yes No

Detailed Descriptions of HTTP Methods

1. GET

  • Description: Used to retrieve data from a server. Does not modify the resource.
  • Characteristics:
    • Idempotent: Yes (multiple identical requests yield the same result).
    • Safe: Yes (does not alter server state).
    • Cacheable: Yes (can be cached by browsers or intermediaries).

Example:

GET /users HTTP/1.1
Host: example.com

2. POST

  • Description: Sends data to the server, often to create a resource or trigger processing.
  • Characteristics:
    • Idempotent: No (repeated requests can create duplicate resources).
    • Safe: No (alters server state).
    • Cacheable: No (response is typically not cached).

Example:

POST /users HTTP/1.1
Host: example.com
Content-Type: application/json

{
  "name": "Ravi",
  "age": 30
}

3. PUT

  • Description: Replaces the entire target resource with the request payload.
  • Characteristics:
    • Idempotent: Yes (subsequent requests with the same payload have the same effect).
    • Safe: No.
    • Cacheable: No.

Example:

PUT /users/1 HTTP/1.1
Host: example.com
Content-Type: application/json

{
  "name": "Ravi",
  "age": 35
}

4. DELETE

  • Description: Deletes a resource identified by the URL.
  • Characteristics:
    • Idempotent: Yes (deleting a resource multiple times has the same result).
    • Safe: No.
    • Cacheable: No.

Example:

DELETE /users/1 HTTP/1.1
Host: example.com

5. HEAD

  • Description: Similar to GET but retrieves only headers (no response body). Useful for checking resource availability.
  • Characteristics:
    • Idempotent: Yes.
    • Safe: Yes.
    • Cacheable: Yes.

Example:

HEAD /users HTTP/1.1
Host: example.com

6. OPTIONS

  • Description: Returns supported HTTP methods for a resource.
  • Characteristics:
    • Idempotent: Yes.
    • Safe: Yes.
    • Cacheable: No.

Example:

OPTIONS /users HTTP/1.1
Host: example.com

7. PATCH

  • Description: Partially updates a resource, allowing modification of specific fields.
  • Characteristics:
    • Idempotent: No (behavior depends on server implementation).
    • Safe: No.
    • Cacheable: No.

Example:

PATCH /users/1 HTTP/1.1
Host: example.com
Content-Type: application/json

{
  "age": 40
}

8. CONNECT

  • Description: Establishes a network connection tunnel, often used for HTTPS requests.
  • Characteristics:
    • Idempotent: No.
    • Safe: No.
    • Cacheable: No.

Example:

CONNECT example.com:443 HTTP/1.1
Host: example.com

9. TRACE

  • Description: Performs a diagnostic loop-back test to see how a request travels through intermediaries.
  • Characteristics:
    • Idempotent: Yes.
    • Safe: Yes.
    • Cacheable: No.

Example:

TRACE /users HTTP/1.1
Host: example.com

Key Terms

  • Idempotent: Repeating the same request yields the same result.
  • Safe: Does not alter the state of the server.
  • Cacheable: The response can be cached for future use.

Best Practices

  1. Use GET for retrieving resources, ensuring it does not alter server state.
  2. Reserve POST for operations that result in data creation or state changes.
  3. Use PUT for full resource updates and PATCH for partial updates.
  4. Implement HEAD and OPTIONS for API introspection and debugging.
  5. Avoid using TRACE and CONNECT unless absolutely necessary, as they are less commonly supported.

Conclusion

HTTP methods form the foundation of communication in web applications. Understanding their purposes and properties helps in designing efficient, RESTful APIs. Use this cheat sheet as a quick reference to ensure proper usage and compliance with best practices. Mastering these methods will enhance your ability to create and consume APIs effectively.

Comments