Sobes.tech
Senior

What is the difference between PUT and POST methods? How do they behave when the same request is sent repeatedly? What does PATCH do?

sobes.tech AI

Answer from AI

HTTP PUT and POST methods differ in semantics and behavior:

  • PUT — an idempotent method used for completely updating a resource at a specified URL. Sending the same PUT request multiple times will not change the result, as the resource will be overwritten with the same data.

  • POST — a non-idempotent method, typically used for creating new resources or performing actions. Resubmitting a POST request may lead to duplicate creation or repeated execution of an operation.

  • PATCH — used for partial updates of a resource, sending only the changed fields. Unlike PUT, which replaces the entire resource, PATCH applies changes to an existing object.

Example:

PUT /users/123
{
  "name": "Ivan",
  "age": 30
}

PATCH /users/123
{
  "age": 31
}

POST /users
{
  "name": "Ivan",
  "age": 30
}

Resubmitting the same PUT will not change the resource, resubmitting POST will create a new user, and resubmitting PATCH will update the age again.

What is the difference between PUT and POST methods… - sobes.tech