Middle+
What is the difference between POST, PUT, and PATCH?
sobes.tech AI
Answer from AI
POST, PUT, and PATCH are HTTP methods used to send data to the server, but with different semantics:
-
POST — creates a new resource. Usually used for adding new data. Each POST call can create a unique new resource.
-
PUT — completely replaces an existing resource or creates it if it does not exist. Idempotent, meaning repeated requests with the same data will not change the result.
-
PATCH — partially updates a resource. Used when only some fields need to be changed, not the entire object.
Example:
POST /users
{
"name": "Ivan",
"age": 30
}
PUT /users/123
{
"name": "Ivan",
"age": 31
}
PATCH /users/123
{
"age": 31
}
Here, POST creates a new user, PUT completely replaces the user with id 123, and PATCH only changes the age of the user with id 123.