PUT vs POST in REST

Source: Internet
Author: User
Tags rfc

From:http://stackoverflow.com/questions/630453/put-vs-post-in-rest

According to the http/1.1 Spec:

The POST method is used to request the origin server accept the entity enclosed in the request as a new sub Ordinate of the resource identified by the Request-URIRequest-Line

In other words, was POST used to create.

The PUT method requests that the enclosed entity is stored under the supplied Request-URI . If Request-URI The refers to an already existing resource, the enclosed entity should is considered as a modified version of the One residing on the origin server. If Request-URI The does not a-existing resource, and that URI was capable of being defined as a new resource by the Req Uesting User agent, the origin server can create the resource with that URI. "

That's, is PUT used to create or update.

So, which one should is used to create a resource? Or one needs to the support both?

Overall:

Both PUT and POST can used for creating.

You have a to ask ' what is performing the action to? ' to distinguish, what is your should be using.  Let's assume you ' re designing a API for asking questions. If you want to use POST and then you would do, to a list of questions. If you want to use PUT then you would do, to a particular question.

Great both can used, so which one should I use in my RESTful design:

You don't need to support both PUT and POST.

Which is used are left up to you. But just remember to use the right one depending on what object is referencing in the request.

Some Considerations:

    • Do you name your URLs 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 idempotent, so if you PUT a object twice, it has no effect. This was a nice property, so I would use PUT when possible.
    • Can update or create a resource with PUT with the same object URL
    • With POST you can has 2 requests coming in the same time making modifications to a URL, and they may update different Parts of the object.

An example:

I wrote the following as part of another answer on so regarding this:

POST:

Used to modify and update a resource

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

Note that the following are an error:

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

If The URL is not a yet created, you should isn't a using POST to create it while specifying the name.  This should result of 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 does something like this to create a resources using POST:

POST /questions HTTP/1.1Host: wahteverblahblah.com

Note: The resource name is not specified, and the new objects URL path would be returned.

PUT:

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

For a new resource:

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

To overwrite an existing resource:

PUT /questions/<existing_question> HTTP/1.1Host: wahteverblahblah.com

You can find assertions on the web that say

    • POST should is used to create a resource, and PUT should is used to modify one
    • PUT should is used to create a resource, and POST should is used to modify one

Neither is quite right.

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 thi  Ng.  By definition, a PUT is idempotent. Do it as many, and the result is the same.  Is x=5 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 an idempotent, in the the-the-that-is-not x++ idempotent.

By this argument, PUT was for creating if you know the URL of the thing you'll create. POST can used to create when you know the URL of the ' factory ' or manager for the category of things you want to create .

So

POST /expense-report

Or

PUT  /expense-report/10929

POST to a URL creates a child resource at a server defined URL.

    • PUT to a URLs creates/replaces the resource in their entirety at the client defined URL.
    • PATCH to a URL for the updates part of the resource on that client defined URL.

The relevant specification for PUT and POST are RFC 2616§9.5ff.

Post creates a child resource, so POST to /items creates a resources that lives under the /items resource. Eg. /items/1. Sending the same post packet twice would create the resources.

PUT is for creating or replacing a resource at a URL knownby the client.

Therefore: PUT is only a candidate for CREATE where the client already knows the URL before the resource is creat Ed. Eg. As the title is used as the /blogs/nigel/entry/when_to_use_post_vs_put resource key

PUT replaces the resource at the known URL if it already exists, so sending the same request twice have no effec T. In and words, calls to PUT is idempotent.

The RFC reads like this:

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 would handle the enclosed entity. That resource might is a data-accepting process, a gateway to some other protocol, or a separate entity that accepts Annot Ations. In contrast, the URI of 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. If the server desires that the request is applied to a different URI,

Note: PUT have mostly been used to update resources (by replacing them in their entireties), but recently there is movement Towar DS using PATCH for updating existing resources, as PUT specifies that it replaces the whole resource. RFC 5789.

PUT vs POST in REST

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.