Sobes.tech
Middle

Mis vahe on PUT ja PATCH REST API-s?

sobes.tech AI

Vastus AI-lt

In REST API, the methods PUT and PATCH are used to update resources, but they differ in meaning and behavior:

  • PUT — complete update of the resource. When calling PUT, the client sends a full representation of the resource, and the server replaces the existing resource with the sent one. If some fields are not specified, they are usually deleted or removed.

  • PATCH — partial update of the resource. The client sends only the fields that need to be changed, and the server updates only those, leaving the others unchanged.

Example:

PUT /users/123
{
  "name": "Ivan",
  "email": "ivan@example.com"
}

Will replace the entire user profile.

PATCH /users/123
{
  "email": "new_email@example.com"
}

Will update only the email, with other data remaining unchanged.