PUT vs POST in RESTful services?


Hello, I would like to know the difference between PUT & POST in Restful services.

As I am using Web API, I feel put and post are same or almost same but doesn't have any idea about when to which one.


Asked by:- Vinnu
0
: 302 At:- 3/28/2018 1:06:30 PM
rest web api put post






2 Answers
profileImage Answered by:- pika

 

HTTP Put and post are not equivalent to Create/Update in CRUD operations.

You should use Post to create a new data, suppose for example you are creating a new article, you can pass data using POST

So use POST when you don't have any idea where to store it, you can POST it to an URL, and let the server decide the actual URL.

POST /articles HTTP/1.1
<article>
    <title>Testing POST</title>
    <price currency="eur">7.50</price>
</article>

HTTP/1.1 201 Created
Location: /articles/1212

Use PUT when you have an actual location or you can say complete URL with Id to update data for an article(as in above example), you can use PUT

PUT /article/1234 HTTP/1.1
<article>
    <title>Updated title</title>
    <price currency="eur">12.50</price>
</article>

As soon as you know the new resource location, you can use PUT again to do updates, you can also use PUT for adding new data.

PUT is idempotent, while POST is not.

So, 

Better is to choose between PUT and POST based on idempotence of the action.

PUT implies putting a resource - completely replacing whatever is available at the given URL with a different thing. By definition, a PUT is idempotent. Do it as many times as you like, and the result is the same. x=5 is idempotent. You can PUT a resource whether it previously exists, or not (eg, to Create, or to Update)!

POST updates a resource, adds a subsidiary resource or causes a change. A POST is not idempotent, in the way that x++ is not idempotent.

by consider above argument, we can say, PUT is for creating when you know the URL of the thing you will create. POST can be used to create when you know the URL of the "factory" or manager for the category of things you want to create.

3
At:- 3/29/2018 9:21:14 AM
now my concepts are clear regarding PUT & POST, thanks 0
By : Vinnu - at :- 4/13/2018 10:46:58 AM


profileImage Answered by:- neena

If you take a look at page 55 of RFC 2616 (“Hypertext Transfer Protocol – HTTP/1.1”), There’s also a handy paragraph to explain the difference between POST and PUT:

The fundamental difference between the POST and PUT requests is reflected in the different meaning of the Request-URI. The URI in a POST request identifies the resource that will handle the enclosed entity. That resource might be a data-accepting process, a gateway to some other protocol, or a separate entity that accepts annotations. In contrast, the URI in a PUT request identifies the entity enclosed with the request – the user agent knows what URI is intended and the server MUST NOT attempt to apply the request to some other resource.

But it doesn’t mention anything about the difference between updating/creating, because that’s not what it’s about. It’s about the difference between this:

obj.set_attribute(value) # A POST request. 

And this:

obj.attribute = value # A PUT request.  

Reference Link

2
At:- 4/11/2018 9:11:21 AM





Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use

Subscribe Now

Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly