Cheat Sheet #day10 - HTTP Methods
Cloud Tuned
3 min read
HTTP Methods Cheat Sheet
HTTP methods define the action to be performed for a given resource in a web application. Here's a quick reference to the most commonly used HTTP methods and their typical use cases.
GET
- Purpose: Retrieve data from a server.
- Characteristics:
- Request data is sent in the URL.
- Should be idempotent (same request can be made multiple times without changing the result).
- Safe operation (does not modify resources).
- Example:
GET /api/users HTTP/1.1 Host: cloudtuned.com
POST
- Purpose: Submit data to a server to create a new resource.
- Characteristics:
- Data is sent in the request body.
- Not idempotent (submitting the same data multiple times may result in multiple resources being created).
Example:
POST /api/users HTTP/1.1 Host: cloudtuned.com Content-Type: application/json { "name": "John Doe", "email": "john.doe@cloudtuned.com" }
PUT
- Purpose: Update an existing resource or create a new one if it does not exist.
- Characteristics:
- Data is sent in the request body.
- Idempotent (multiple identical requests will result in the same resource state).
Example:
PUT /api/users/1 HTTP/1.1 Host: cloudtuned.com Content-Type: application/json { "name": "John Doe", "email": "john.doe@cloudtuned.com" }
PATCH
- Purpose: Apply partial modifications to a resource.
- Characteristics:
- Data is sent in the request body.
- Not necessarily idempotent.
Example:
PATCH /api/users/1 HTTP/1.1 Host: cloudtuned.com Content-Type: application/json { "email": "john.new@cloudtuned.com" }
DELETE
- Purpose: Remove a resource from the server.
- Characteristics:
- Idempotent (multiple identical delete requests will have the same effect).
- Example:
DELETE /api/users/1 HTTP/1.1 Host: cloudtuned.com
HEAD
- Purpose: Retrieve the headers for a resource, without the response body.
- Characteristics:
- Used for checking what a GET request will return before making the actual request.
- Idempotent and safe.
- Example:
HEAD /api/users HTTP/1.1 Host: cloudtuned.com
OPTIONS
- Purpose: Describe the communication options for the target resource.
- Characteristics:
- Used for CORS preflight requests.
- Safe and idempotent.
- Example:
OPTIONS /api/users HTTP/1.1 Host: cloudtuned.com
TRACE
- Purpose: Perform a message loop-back test along the path to the target resource.
- Characteristics:
- Used mainly for diagnostic purposes.
- Safe and idempotent.
- Example:
TRACE /api/users HTTP/1.1 Host: cloudtuned.com
CONNECT
- Purpose: Establish a tunnel to the server identified by the target resource.
- Characteristics:
- Used mainly for SSL tunneling.
- Example:
CONNECT cloudtuned.com:443 HTTP/1.1 Host: cloudtuned.com
Summary
- Safe Methods: GET, HEAD, OPTIONS, TRACE
- Idempotent Methods: GET, PUT, DELETE, HEAD, OPTIONS, TRACE
- Common Use Cases:
- GET: Fetching data, reading resources.
- POST: Creating new resources, submitting forms.
- PUT: Updating or replacing resources.
- PATCH: Partially updating resources.
- DELETE: Removing resources.
Understanding these HTTP methods and their appropriate use cases is crucial for designing RESTful APIs and handling web requests effectively.
0
Subscribe to my newsletter
Read articles from Cloud Tuned directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by