- The PUT method is called when you need to update a resource, and the POST method is called when you need to add a resource.
- In the PUT method, the resource being updated must have a unique URI, and in the POST method, the resource being added may not yet have a URI.
- If you send the same HTTP PUT request multiple times, the server state remains the same, but if you send the same HTTP POST request multiple times, you might get a different server state after each request.
What is the HTTP PUT method, and what is it used for?
The HTTP PUT request method creates a new resource or replaces the target resource with the request payload. It puts a file or resource at a specific URI. If there is already a resource in this URI, PUT replaces it. If there is no resource there, PUT creates it. PUT is idempotent and not cachable.
What is the HTTP POST method, and what is it used for?
The HTTP POST request method is used to upload files, submit HTML forms, create or update resources on the webserver with the request payload. POST does not require the resource to have a unique URI. POST is not idempotent but is cachable.
When to use PUT and when to use POST?
The choice between using PUT and POST should be based on the idempotency of the operation. Below is a comparison table of the HTTP PUT and POST methods.
|PUT Request||POST Request|
|If the PUT request URI refers to an existing resource, then an update operation must be performed; otherwise, a create operation must be performed.||The POST method is used to create new resource on the server. If the request URI refers to an existing resource, then POST updates the resource.|
|The PUT method is idempotent. If the same PUT request is sent two or more times, then the server state should remain the same as after the first request.||POST is not idempotent. If the same POST request is sent two or more times, then the state of the server may be different after each request.|
|PUT responses are not cacheable.||POST responses can be cached if they contain the appropriate Cache-Control or Expires HTTP headers.|
|PUT method overwrites the existing resource on the server.||POST method adds a new resource to the server.|
|PUT corresponds to the UPDATE method for CRUD operations.||POST corresponds to the CREATE method for CRUD operations.|
HTTP PUT Request ExampleExample of sending a PUT request to ReqBin echo URL:
HTTP POST Request ExampleExample of sending a POST request to ReqBin echo URL:
PUT vs POST Summary
Both POST and PUT are popular HTTP methods that can be easily confused or used interchangeably. Your choice should be based on the idempotency of the operation. Otherwise, misuse of the method can lead to unexpected errors.