HTTP Methods Tutorial
HTTP (Hypertext Transfer Protocol) defines a set of request methods to indicate the desired action to be performed for a given resource. These methods are fundamental to web development and RESTful APIs.
1. GET
- Purpose: Retrieve data from the server.
- Characteristics:
- Safe and idempotent (does not modify data).
- Parameters are sent in the URL (query string).
- Example:
GET /users?id=123
2. POST
- Purpose: Submit data to the server to create a resource.
- Characteristics:
- Not idempotent (can create multiple resources).
- Data is sent in the request body.
- Example:
POST /userswith JSON body{ "name": "Alice" }
3. PUT
- Purpose: Update or replace a resource.
- Characteristics:
- Idempotent (repeated requests have the same effect).
- Data is sent in the request body.
- Example:
PUT /users/123with JSON body{ "name": "Bob" }
4. PATCH
- Purpose: Partially update a resource.
- Characteristics:
- Not necessarily idempotent.
- Only the fields to update are sent in the request body.
- Example:
PATCH /users/123with JSON body{ "email": "bob@example.com" }
5. DELETE
- Purpose: Remove a resource from the server.
- Characteristics:
- Idempotent.
- Example:
DELETE /users/123
6. HEAD
- Purpose: Same as GET, but only retrieves headers (no body).
- Use Case: Check if a resource exists, get metadata.
7. OPTIONS
- Purpose: Describe the communication options for the target resource.
- Use Case: Discover allowed methods, CORS preflight requests.
8. TRACE
- Purpose: Echo the received request, mainly for debugging.
9. CONNECT
- Purpose: Establish a tunnel to the server, typically for HTTPS.
Summary Table
| Method | Safe | Idempotent | Typical Use |
|---|---|---|---|
| GET | Yes | Yes | Retrieve data |
| POST | No | No | Create resource |
| PUT | No | Yes | Replace resource |
| PATCH | No | No/Yes | Partial update |
| DELETE | No | Yes | Delete resource |
| HEAD | Yes | Yes | Headers only |
| OPTIONS | Yes | Yes | Discover methods |
| TRACE | Yes | Yes | Debugging |
| CONNECT | No | No | Tunnel (e.g., HTTPS proxy) |