What are the differences between PUT and POST in REST?

DWQA QuestionsCategory: Web Design HTMLWhat are the differences between PUT and POST in REST?
Editor Staff asked 3 months ago

What are the differences between PUT and POST in REST?

1 Answers
Editor Staff answered 3 months ago

PUT
POST

PUT methods are used to request the server to store the enclosed entity in request. In case, the request does not exist, then new resource has to be created. If the resource exists, then the resource should get updated.
POST method is used to request the server to store the enclosed entity in the request as a new resource.

The URI should have a resource identifier. Example: PUT /users/{user-id}
The POST URI should indicate the collection of the resource. Example: POST /users

PUT methods are idempotent.
POST methods are not idempotent.

PUT is used when the client wants to modify a single resource that is part of the collection. If a part of the resource has to be updated, then PATCH needs to be used.
POST methods are used to add a new resource to the collection.

The responses are not cached here despite the idempotency.
Responses are not cacheable unless the response explicitly specifies Cache-Control fields in the header.

In general, PUT is used for UPDATE operations.
POST is used for CREATE operations.