What is the difference between the PUT method and the PATCH method?
sobes.tech AI
Answer from AI
HTTP methods PUT and PATCH are used for updating resources but differ in their semantics and scope of update:
-
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. If the resource does not exist, it can be created.
-
PATCH is used for partial updates of a resource. The client sends only the fields or changes that need to be applied, and the server updates the resource partially without affecting other fields.
Example:
PUT /users/123
{
"name": "Ivan",
"age": 30
}
Replaces the entire user resource with id 123 with a new object.
PATCH /users/123
{
"age": 31
}
Updates only the age field without changing the name.
Thus, PUT is for full replacement, PATCH is for partial updates.