Middle+
What is the difference between PATCH and PUT?
sobes.tech AI
Answer from AI
The main difference between PATCH and PUT lies in how they update resources:
-
PUT is an operation that completely replaces a resource. When using PUT, the client sends a full representation of the resource, and the server replaces the existing resource with this new state. If some fields are not specified, they are usually considered deleted or reset.
-
PATCH is a partial update operation. The client sends only the fields that need to be changed, and the server updates only those fields, leaving the others unchanged.
Example:
If you have a user object with fields name and email:
- PUT with body
{ "name": "Ivan", "email": "ivan@example.com" }will replace the entire object. - PATCH with body
{ "email": "ivan_new@example.com" }will only change the email, leaving the name unchanged.
Thus, PATCH is more flexible for partial changes, while PUT is used for full resource replacement.