Junior — Middle
Explain the difference between HTTP PUT and PATCH methods in the context of resource updates.
sobes.tech AI
Answer from AI
HTTP methods PUT and PATCH are both used for updating resources, but they differ in meaning and behavior:
-
PUT is an idempotent method that replaces the entire resource. When you call PUT, you send a complete representation of the resource, and the server replaces the existing resource with it.
-
PATCH is used for partial updates. You send only the fields you want to change, and the server updates them without touching the others.
Example:
- PUT /users/1 with body {"name":"Ivan", "age":30} will replace the entire user profile.
- PATCH /users/1 with body {"age":31} will only change the age, leaving the name unchanged.
Thus, PUT is suitable for full replacement, while PATCH is for partial modifications.