Understand HTTP Methods
HTTP methods define the operation that a client wants to perform on a resource.
The most commonly used methods in REST API development are GET, POST, PUT, PATCH and DELETE.
GET retrieves information from the server.
Example:
GET /api/users
POST creates a new resource.
Example:
POST /api/users
PUT replaces an existing resource with a new representation.
Example:
PUT /api/users/123
PATCH partially updates an existing resource.
Example:
PATCH /api/users/123
DELETE removes a resource.
Example:
DELETE /api/users/123
Understanding the difference between PUT and PATCH is particularly important.
Suppose a user document contains:
{
"name": "John",
"email": "john@example.com",
"age": 25
}
If you use PUT, the request generally provides the complete replacement representation.
If you use PATCH, you can update only the age:
{
"age": 26
}
HTTP methods also have different safety and idempotency properties. GET is safe and idempotent. PUT and DELETE are idempotent by HTTP semantics. POST is generally neither safe nor inherently idempotent.
HTTP Methods Comparison
This comparison matrix has incomplete data.
GET retrieves resources.
POST creates resources.
PUT replaces resources.
PATCH partially updates resources.
DELETE removes resources.