Sobes.tech
Junior — Middle

What is the difference between HTTP PUT and PATCH requests when updating resources?

sobes.tech AI

Answer from AI

HTTP PUT and PATCH requests are used for updating resources but differ in meaning and application:

  • PUT — complete update of a resource. When sending a PUT, usually the full set of resource data is transmitted, and the server replaces the existing resource with the new one. If the resource does not exist, it can be created.

  • PATCH — partial update of a resource. Only the set of changes or fields to be updated are sent, without replacing the entire object.

Example:

PUT /users/123
Content-Type: application/json

{
  "name": "Ivan",
  "email": "ivan@example.com"
}

Will replace the entire user 123 resource with new data.

PATCH /users/123
Content-Type: application/json

{
  "email": "newemail@example.com"
}

Will only update the email field of user 123.

The choice depends on the task: use PUT if you need to replace the entire object, use PATCH to update a part.