PUT vs POST: What's the Difference?

What is PUT?

PUT method is used to update resource available on the server. Typically, it replaces whatever exists at the target URL with something else. You can use it to make a new resource or overwrite an existing one. PUT requests that the enclosed entity must be stored under the supplied requested URI (Uniform Resource Identifier).

In this tutorial, you will learn:

What is POST?

POST is a method that is supported by HTTP and

depicts that a web server accepts the data included in the body of the message, which is requested. POST is often used by World Wide Web to send user generated data to the web server or when you upload file.

KEY DIFFERENCES:

  • PUT method is called when you have to modify a single resource while POST method is called when you have to add a child resource.
  • PUT method response can be cached but you cannot cache PUT method responses.
  • You can use UPDATE query in PUT whereas you can use create query in POST.
  • In PUT method, the client decides which URI resource should have, and in POST method, the server decides which URI resource should have.
  • PUT works as specific while POST work as abstract.
  • If you send the same PUT request multiple times, the result will remain the same but if you send the same POST request multiple times, you will receive different results.
  • PUT method is idempotent whereas POST method is not idempotent.

Example of PUT

Here is the webserver example of a PUT method:

HTTP PUT http://www.google.com/users/234

HTTP PUT http://www.google.com/users/234/accounts/567

Request

PUT /new.html HTTP/1.1
Host: example.com
Content-type: text/html
Content-length: 20

<p>New File</p>

Responses

If the target resource having current representation and is modified with the state of the enclosed representation, then the server should send two responses. The first response code is 200 (OK), and the second response code is 204 (No Content).

If the target resource does not have any representation, then the server should inform the user by sending a 201 code (Created) response.

 HTTP/1.1 201 Created
Content-Location: /new.html

Example of POST

Here is an example of POST method:

HTTP POST http://www.google.com/users

HTTP POST http://www.google.com/users/234/accounts

A form using the default application/x-www-form-urlencoded content type:

POST /test HTTP/1.1
Host: abc.example
Content-Type: application/x-www-form-urlencoded
Content-Length: 40

field1=value1&field2=value2

Difference between PUT and POST

Here is the important difference between PUT and POST method:

PUT POST
This method is idempotent. This method is not idempotent.
PUT method is call when you have to modify a single resource, which is already a part of resource collection. POST method is call when you have to add a child resource under resources collection.
RFC-2616 depicts that the PUT method sends a request for an enclosed entity stored in the supplied request URI. This method requests the server to accept the entity which is enclosed in the request.
PUT method syntax is PUT /questions/{question-id} POST method syntax is POST /questions
PUT method answer can be cached. You cannot cache PUT method responses.
PUT /vi/juice/orders/1234 indicates that you are updating a resource which is identified by "1234". POST /vi/juice/orders indicates that you are creating a new resource and return an identifier to describe the resource.
If you send the same request multiple times, the result will remain the same. If you send the same POST request more than one time, you will receive different results.
PUT works as specific. POST work as abstract.
We use UPDATE query in PUT. We use create query in POST.
In PUT method, the client decides which URI resource should have. In POST method, the server decides which URI resource should have.

Testing an API with PUT requests

Here are the steps to test API with PUT requests:

Testing an API with PUT requests

Step 1) Update resources with PUT request.

Step 2) Use GET method for resource. If the PUT request success, you will receive new data. This method will fail if the supplied data in the request is invalid. Therefore, it will not update anything.

Testing an API with POST requests

Here are the steps to test API with POST requests:

Testing an API with POST requests

Step 1) Create a resource using POST request and make sure that it returns 200 status code.

Step 2) Make a GET request for that resource and save the data in the correct format.

Step 3) You have to add tests which ensure POST requests fail with incorrect data.

Advantages of PUT Method

Here are pros/benefits of using PUT method:

Advantages of POST Method

Here are pros/benefits of using POST method:

 

YOU MIGHT LIKE: