HTTP methods (also known as verbs) define the type of operation to perform on a resource in RESTful APIs. Each method has a specific purpose and is used in different scenarios based on the action required.
1. GET
Purpose
Used to retrieve data from a server.
Typically used for fetching resources without making any modifications on the server.
Characteristics
Idempotent: Multiple identical requests produce the same result.
Safe: Does not alter server state.
Cacheable: Responses can often be cached for better performance.
Use Cases
Fetching a list of resources:
/api/products
(all products).Fetching a specific resource:
/api/products/1
(product with ID 1).
Example
Request:
GET /api/users HTTP/1.1
Host: example.com
Response:
[
{ "id": 1, "name": "Alice" },
{ "id": 2, "name": "Bob" }
]
2. POST
Purpose
- Used to send data to a server to create a new resource.
Characteristics
Not Idempotent: Multiple identical requests create multiple resources.
Safe for creating resources: Can include detailed information in the body.
Use Cases
Creating a new user:
/api/users
with details like name, email.Submitting forms: Registration, feedback, or data entry forms.
Example
Request:
POST /api/users HTTP/1.1
Content-Type: application/json
{
"name": "Charlie",
"email": "charlie@example.com"
}
Response:
{
"id": 3,
"name": "Charlie",
"email": "charlie@example.com"
}
3. PUT
Purpose
- Used to update or replace an existing resource on the server.
Characteristics
Idempotent: Multiple identical requests result in the same state of the resource.
Typically used when updating entire resources.
Use Cases
- Replacing an entire resource: Updating all details of a user or product.
Example
Request:
PUT /api/users/1 HTTP/1.1
Content-Type: application/json
{
"name": "Alice",
"email": "alice_new@example.com"
}
Response:
{
"id": 1,
"name": "Alice",
"email": "alice_new@example.com"
}
4. DELETE
Purpose
- Used to remove a resource from the server.
Characteristics
- Idempotent: Multiple identical DELETE requests achieve the same outcome (the resource is removed).
Use Cases
Deleting a specific resource:
/api/users/1
(delete user with ID 1).Bulk deletion: Deleting a set of records based on criteria.
Example
Request:
DELETE /api/users/1 HTTP/1.1
Response:
HTTP/1.1 204 No Content
5. PATCH
Purpose
- Used to apply partial updates to a resource.
Characteristics
Not Idempotent by default: Depending on the server implementation, may or may not be idempotent.
Preferred for updating specific fields instead of the entire resource.
Use Cases
- Updating a single property of a resource: Changing a user's email while keeping other details unchanged.
Example
Request:
PATCH /api/users/1 HTTP/1.1
Content-Type: application/json
{
"email": "alice_patch@example.com"
}
Response:
"id": 1,
"name": "Alice",
"email": "alice_patch@example.com"
}
Comparison of PUT and PATCH
Feature | PUT | PATCH |
Scope | Replaces the entire resource. | Updates part of the resource. |
Request Payload | Requires complete resource details. | Requires only the fields to update. |
Idempotence | Always idempotent. | May or may not be idempotent. |