What is the difference between POST and PUT in HTTP

0 votes

I am aware that POST is used to create a resource. The POST method is used to request that the origin server accepts the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line.

On the other hand, I’m aware that PUT is used to create or replace a resource. The PUT method requests that the enclosed entity be stored under the supplied Request-URI. If the Request-URI refers to an already existing resource, the enclosed entity SHOULD be considered as a modified version of the one residing on the origin server. If the Request-URI does not point to an existing resource, and that URI is capable of being defined as a new resource by the requesting user agent, the origin server can create the resource with that URI.

So which HTTP method should be used to create a resource? Or should both be supported?

Feb 11, 2022 in Others by Soham
• 9,710 points
762 views

1 answer to this question.

0 votes

To answer your question, both of them can be used for the creation process. All you have to do is ask “what are you performing the action upon?” to differentiate what you should be using. Let us assume that what you're designing an API for, is for asking questions. If you want to use POST, then you would do that to a list of questions. If you want to use PUT, then you would do that to a particular question.

Some considerations:

  • Do you name the URL objects you create explicitly, or let the server decide? If you name them then use PUT. If you let the server decide then use POST.

  • PUT is defined to assume idempotency, so if you PUT an object twice, it should have no additional effect. This is a nice property, so I would use PUT when possible. Just make sure that the PUT-idempotency actually is implemented correctly in the server.

  • You can update or create a resource with PUT with the same object URL

  • With POST you can have 2 requests coming in at the same time making modifications to a URL, and they may update different parts of the object.


POST:

Used to modify and update a resource

POST /questions/<existing_question> HTTP/1.1 
Host: www.example.com/

Note that the following is an error:
 

POST /questions/<new_question> HTTP/1.1 Host: www.example.com/

If the URL is not yet created, you should not be using POST to create it while specifying the name. This should result in a 'resource not found' error because <new_question> does not exist yet. You should PUT the <new_question> resource on the server first.

You could though do something like this to create a resources using POST:

POST /questions HTTP/1.1 
Host: www.example.com/

Note that in this case the resource name is not specified, the new objects URL path would be returned to you.
 

PUT:

Used to create a resource, or overwrite it. While you specify the resource's new URL.
 

For a new resource:
 

PUT /questions/<new_question> HTTP/1.1 
Host: www.example.com/

To overwrite an existing resource:

PUT /questions/<existing_question> HTTP/1.1 
Host: www.example.com/
answered Feb 11, 2022 by Rahul
• 9,680 points

Related Questions In Others

+1 vote
1 answer

what is the difference between error and stderr in Node.js?

Error is an object created by Node.js to handle ...READ MORE

answered Jul 4, 2019 in Others by sunshine
• 1,300 points
2,393 views
0 votes
0 answers

What is the difference between iostream , stdio.h and conio.h header files in c++?

I am confused between all these three ...READ MORE

Sep 23, 2022 in Others by gaurav
• 23,260 points
1,500 views
0 votes
1 answer

What is the difference between hadoop and google analytics ?

I will try and answer this as ...READ MORE

answered Aug 22, 2018 in Others by Frankie
• 9,830 points
1,591 views
+1 vote
1 answer

What is the difference between Dark Web and Deep Web?

The content that which you cannot find ...READ MORE

answered Feb 6, 2019 in Others by Priyaj
• 58,020 points
1,182 views
0 votes
0 answers

400 BAD request HTTP error code meaning?

I am posting a JSON request to ...READ MORE

May 1, 2022 in Other DevOps Questions by Kichu
• 19,040 points
1,333 views
0 votes
2 answers

Send HTTP request in Java

import com.google.api.client.http.GenericUrl; import com.google.api.client.http.HttpRequest; import com.google.api.client.http.HttpResponse; import com.google.api.client.http.HttpTransport; import com.google.api.client.http.javanet.NetHttpTransport; import java.io.IOException; import ...READ MORE

answered Aug 3, 2018 in Java by samarth295
• 2,220 points
1,746 views
0 votes
1 answer

How to get issues count based on rules in a sonar project?

There are API docs in the footer ...READ MORE

answered May 4, 2018 in Other DevOps Questions by DareDev
• 6,890 points
2,843 views
0 votes
1 answer

Any rate limiter for kitura

Try using KituraLimiter as a rate limiting ...READ MORE

answered Jun 4, 2018 in DevOps Tools by DareDev
• 6,890 points
645 views
0 votes
1 answer

can somebody explain me what does "passing by value" and "Passing by reference" mean in C#?

To answer your question, “passing by value” ...READ MORE

answered Feb 10, 2022 in Others by Rahul
• 9,680 points
781 views
0 votes
1 answer

HTTP Error 403.14 - Forbidden - The Web server is configured to not list the contents of this directory

Try keeping this into your web config ...READ MORE

answered Feb 11, 2022 in Others by Rahul
• 9,680 points
12,941 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP