Junior — Middle
What is the main difference between HTTP PUT and PATCH methods when updating resources?
sobes.tech AI
Answer from AI
The main difference between HTTP methods PUT and PATCH lies in how they update a resource:
-
PUT is an idempotent method that replaces the entire resource. When using PUT, the client sends a complete representation of the resource, and the server replaces the existing resource with this new state.
-
PATCH is a method for partial updates. The client sends only the fields or changes that need to be applied to the resource, and the server updates only those parts, leaving the rest unchanged.
Example:
If you have a user resource with fields name and email:
- A PUT request should contain both fields, for example:
{
"name": "Ivan",
"email": "ivan@example.com"
}
- A PATCH request can contain only the field to be changed, for example:
{
"email": "new_email@example.com"
}
Thus, PUT replaces the entire resource, while PATCH updates it partially.