Junior — Middle
Explain what the PUT method in REST API means and in which cases it is used.
sobes.tech AI
Answer from AI
The PUT method in REST API is used for completely updating a resource or creating it if it does not exist yet. Unlike PATCH, which applies partial changes, PUT replaces the entire resource with the provided data.
Main features of PUT:
- Idempotency: repeated calls with the same data will not change the result.
- Usually used to update an existing object at a known URI.
- If the resource does not exist, the server can create it with the specified URI.
Example: if you have a user with ID 123, a PUT request to /users/123 with the full user description will replace all fields of this user with new values.
PUT is used when you need to guarantee the complete replacement of a resource, rather than partial updates.