PUT vs. POST in REST












4776















According to the HTTP/1.1 Spec:




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




In other words, POST is used to create.




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."




That is, PUT is used to create or update.



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










share|improve this question




















  • 44





    It may be helpful to use the definitions in HTTPbis - Roy put a fair amount of work into clarifying them. See: tools.ietf.org/html/…

    – Mark Nottingham
    Oct 23 '11 at 21:03








  • 13





    Just to bring @MarkNottingham's comment to the latest revision, here's POST and PUT, as defined on HTTPbis.

    – Marius Butuc
    Nov 18 '12 at 1:58








  • 33





    It seems to me that this debate has arisen from the common practice of oversimplifying REST by describing the HTTP Methods in terms of CRUD operations.

    – Stuporman
    Feb 14 '13 at 17:05






  • 5





    Unfortunally the first answers are wrong about POST. Check my answer for a better explanation of the differences: stackoverflow.com/a/18243587/2458234

    – 7hi4g0
    Nov 25 '13 at 5:21






  • 22





    PUT and POST are both unsafe methods. However, PUT is idempotent, while POST is not. - See more at: restcookbook.com/HTTP%20Methods/put-vs-post/…

    – Dinesh Saini
    Jan 10 '14 at 20:26
















4776















According to the HTTP/1.1 Spec:




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




In other words, POST is used to create.




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."




That is, PUT is used to create or update.



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










share|improve this question




















  • 44





    It may be helpful to use the definitions in HTTPbis - Roy put a fair amount of work into clarifying them. See: tools.ietf.org/html/…

    – Mark Nottingham
    Oct 23 '11 at 21:03








  • 13





    Just to bring @MarkNottingham's comment to the latest revision, here's POST and PUT, as defined on HTTPbis.

    – Marius Butuc
    Nov 18 '12 at 1:58








  • 33





    It seems to me that this debate has arisen from the common practice of oversimplifying REST by describing the HTTP Methods in terms of CRUD operations.

    – Stuporman
    Feb 14 '13 at 17:05






  • 5





    Unfortunally the first answers are wrong about POST. Check my answer for a better explanation of the differences: stackoverflow.com/a/18243587/2458234

    – 7hi4g0
    Nov 25 '13 at 5:21






  • 22





    PUT and POST are both unsafe methods. However, PUT is idempotent, while POST is not. - See more at: restcookbook.com/HTTP%20Methods/put-vs-post/…

    – Dinesh Saini
    Jan 10 '14 at 20:26














4776












4776








4776


2100






According to the HTTP/1.1 Spec:




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




In other words, POST is used to create.




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."




That is, PUT is used to create or update.



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










share|improve this question
















According to the HTTP/1.1 Spec:




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




In other words, POST is used to create.




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."




That is, PUT is used to create or update.



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







http rest post put






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 22 '17 at 11:39









Peter Mortensen

13.7k1986111




13.7k1986111










asked Mar 10 '09 at 14:25









alexalex

30.5k74256




30.5k74256








  • 44





    It may be helpful to use the definitions in HTTPbis - Roy put a fair amount of work into clarifying them. See: tools.ietf.org/html/…

    – Mark Nottingham
    Oct 23 '11 at 21:03








  • 13





    Just to bring @MarkNottingham's comment to the latest revision, here's POST and PUT, as defined on HTTPbis.

    – Marius Butuc
    Nov 18 '12 at 1:58








  • 33





    It seems to me that this debate has arisen from the common practice of oversimplifying REST by describing the HTTP Methods in terms of CRUD operations.

    – Stuporman
    Feb 14 '13 at 17:05






  • 5





    Unfortunally the first answers are wrong about POST. Check my answer for a better explanation of the differences: stackoverflow.com/a/18243587/2458234

    – 7hi4g0
    Nov 25 '13 at 5:21






  • 22





    PUT and POST are both unsafe methods. However, PUT is idempotent, while POST is not. - See more at: restcookbook.com/HTTP%20Methods/put-vs-post/…

    – Dinesh Saini
    Jan 10 '14 at 20:26














  • 44





    It may be helpful to use the definitions in HTTPbis - Roy put a fair amount of work into clarifying them. See: tools.ietf.org/html/…

    – Mark Nottingham
    Oct 23 '11 at 21:03








  • 13





    Just to bring @MarkNottingham's comment to the latest revision, here's POST and PUT, as defined on HTTPbis.

    – Marius Butuc
    Nov 18 '12 at 1:58








  • 33





    It seems to me that this debate has arisen from the common practice of oversimplifying REST by describing the HTTP Methods in terms of CRUD operations.

    – Stuporman
    Feb 14 '13 at 17:05






  • 5





    Unfortunally the first answers are wrong about POST. Check my answer for a better explanation of the differences: stackoverflow.com/a/18243587/2458234

    – 7hi4g0
    Nov 25 '13 at 5:21






  • 22





    PUT and POST are both unsafe methods. However, PUT is idempotent, while POST is not. - See more at: restcookbook.com/HTTP%20Methods/put-vs-post/…

    – Dinesh Saini
    Jan 10 '14 at 20:26








44




44





It may be helpful to use the definitions in HTTPbis - Roy put a fair amount of work into clarifying them. See: tools.ietf.org/html/…

– Mark Nottingham
Oct 23 '11 at 21:03







It may be helpful to use the definitions in HTTPbis - Roy put a fair amount of work into clarifying them. See: tools.ietf.org/html/…

– Mark Nottingham
Oct 23 '11 at 21:03






13




13





Just to bring @MarkNottingham's comment to the latest revision, here's POST and PUT, as defined on HTTPbis.

– Marius Butuc
Nov 18 '12 at 1:58







Just to bring @MarkNottingham's comment to the latest revision, here's POST and PUT, as defined on HTTPbis.

– Marius Butuc
Nov 18 '12 at 1:58






33




33





It seems to me that this debate has arisen from the common practice of oversimplifying REST by describing the HTTP Methods in terms of CRUD operations.

– Stuporman
Feb 14 '13 at 17:05





It seems to me that this debate has arisen from the common practice of oversimplifying REST by describing the HTTP Methods in terms of CRUD operations.

– Stuporman
Feb 14 '13 at 17:05




5




5





Unfortunally the first answers are wrong about POST. Check my answer for a better explanation of the differences: stackoverflow.com/a/18243587/2458234

– 7hi4g0
Nov 25 '13 at 5:21





Unfortunally the first answers are wrong about POST. Check my answer for a better explanation of the differences: stackoverflow.com/a/18243587/2458234

– 7hi4g0
Nov 25 '13 at 5:21




22




22





PUT and POST are both unsafe methods. However, PUT is idempotent, while POST is not. - See more at: restcookbook.com/HTTP%20Methods/put-vs-post/…

– Dinesh Saini
Jan 10 '14 at 20:26





PUT and POST are both unsafe methods. However, PUT is idempotent, while POST is not. - See more at: restcookbook.com/HTTP%20Methods/put-vs-post/…

– Dinesh Saini
Jan 10 '14 at 20:26












32 Answers
32






active

oldest

votes













1 2
next












3756














Overall:



Both PUT and POST can be used for creating.



You have to ask "what are you performing the action to?" to distinguish what you should be using. Let's assume you're designing an API 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.



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



You do not need to support both PUT and POST.



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



Some considerations:




  • Do you name your 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 idempotent, so if you PUT an object twice, it has no effect. This is a nice property, so I would use PUT when possible.

  • 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.


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.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
resources 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/






share|improve this answer





















  • 915





    I think one cannot stress enough the fact that PUT is idempotent: if the network is botched and the client is not sure whether his request made it through, it can just send it a second (or 100th) time, and it is guaranteed by the HTTP spec that this has exactly the same effect as sending once.

    – Jörg W Mittag
    Mar 10 '09 at 15:17






  • 71





    @Jörg W Mittag: Not necessary. The second time could return 409 Conflict or something if the request has been modified in meantime (by some other user or the first request itself, which got through).

    – Mitar
    Nov 27 '11 at 23:28






  • 564





    If I'm not mistaken, what we should be stressing is that PUT is defined to be idempotent. You still have to write your server in such a way that PUT behaves correctly, yes? Perhaps it's better to say "PUT causes the transport to assume idempotence, which may affect behavior of the transport, e.g. caching."

    – Ian Ni-Lewis
    Dec 28 '11 at 2:05








  • 133





    @JörgWMittag Idempotence catchphrase? How about "Send and send and send my friend, it makes no difference in the end."

    – James Beninger
    Mar 3 '14 at 17:13








  • 20





    Thinks of them as: PUT = insert or update; POST = insert. So when you make two PUT - you get the one new record, when you do two POSTs - you get two new records.

    – Eugen Konkov
    Aug 22 '16 at 9:34



















2002














You can find assertions on the web that say





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


  • PUT should be used to create a resource, and POST should be 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 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 this argument, 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.



so:



POST /expense-report


or:



PUT  /expense-report/10929





share|improve this answer





















  • 63





    I agree, wherever idempotence is concerned it should trump any other concerns since getting that wrong can cause many many unexpected bugs.

    – Josh
    Oct 26 '10 at 5:56






  • 13





    If POST can update a resource, how is that not idempotent? If I change a students age using PUT and do that 10x times the students age is the same if I did it once.

    – Schneider
    May 6 '11 at 10:54






  • 23





    @Schneider, in this case your server is making an extra effort to guarantee idempotence, but it is not advertising it. Browsers will still warn the user if they try to reload such a POST request.

    – Tobu
    Jan 6 '12 at 10:53






  • 43





    @Schneider POST may create a subsidiary resource; hence you can POST to collection, like POST /expense-reports and it would create as many entities (expense reports) on your server as the quantity of requests you've sent, even if they are completely similar. Think of it as inserting the same row in the DB table (/expense-reports) with auto-incremented primary key. Data remains the same, key (URI in this case) is generated by server and is different for every other insert (request). So, POST effect can be idempotent, but also may not. Hence, POST is not idempotent.

    – Snifff
    Jan 26 '12 at 17:32








  • 11





    Let's say we have entities which may have two properties - name and date. If we have an entity with an existing name and date, but then make requests to it specifying only a name, the proper behavior of PUT would be to obliterate the date of the entity, whereas POST may update only the properties specified, leaving the unspecified properties as they were before the request was made. Does that sound correct/reasonable, or is it an improper use of PUT (I saw references to PATCH, which it seems would be more appropriate, but doesn't exist yet)?

    – Jon z
    May 8 '13 at 18:28





















619
















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


  • PUT to a URL creates/replaces the resource in its entirety at the client defined URL.


  • PATCH to a URL updates part of the resource at that client defined URL.


The relevant specification for PUT and POST is 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 will create two resources.



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



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



PUT replaces the resource at the known url if it already exists, so sending the same request twice has no effect. In other words, calls to PUT are 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 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. If the server desires that the request be applied to a different URI,




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



Update 2018: There is a case that can be made to avoid PUT. See "REST without PUT"




With “REST without PUT” technique, the idea is that consumers are
forced to post new 'nounified' request resources. As discussed
earlier, changing a customer’s mailing address is a POST to a new
“ChangeOfAddress” resource, not a PUT of a “Customer” resource with a
different mailing address field value.




taken from REST API Design - Resource Modeling by Prakash Subramaniam of Thoughtworks



This forces the API to avoid state transition problems with multiple clients updating a single resource, and matches more nicely with event sourcing and CQRS. When the work is done asynchronously, POSTing the transformation and waiting for it to be applied seems appropriate.






share|improve this answer





















  • 46





    Or from the other side of the fence: PUT if the client determines the resulting resource's address, POST if the server does it.

    – DanMan
    Nov 28 '12 at 19:47






  • 3





    I think that this answer should be edited to make it more clear what @DanMan pointed in a very simple way. What I find the most valuable here is the note at the end, stating that a PUT should be used only for replacing the whole resource.

    – Hermes
    Nov 26 '13 at 22:37






  • 2





    PATCH isn't a realistic option for at least a few years, but I agree with the ideology.

    – crush
    Oct 3 '14 at 17:33






  • 4





    I'm trying to understand, but using PUT to create something would only make sense if the client knows for sure that the resource doesn't exist yet, right? Following the blog example, say you have created hundreds of blog posts in a couple of years, then accidentally pick the same title as you did for a post two years ago. Now you have gone and replaced that post, which wasn't intended. So using PUT to create would require the client to track what is taken and what is not, and could lead to accidents and unintended side effects, as well as having routes that do two entirely different things?

    – galaxyAbstractor
    Jan 30 '15 at 9:01








  • 4





    You are correct. PUTting a blog post at the same url as an existing one would cause an update to that existing post (although you could obviously check first with a GET). This indicates why it would be a bad idea to use just the title as the URL. It would however work anywhere there was a natural key in the data... which in my experience is rare. Or if you used GUIDs

    – Nigel Thorne
    Feb 3 '15 at 5:20





















179














Summary:



Create:



Can be performed with both PUT or POST in the following way:




PUT



Creates THE new resource with newResourceId as the identifier, under the /resources URI, or collection.



PUT /resources/<newResourceId> HTTP/1.1 


POST



Creates A new resource under the /resources URI, or collection. Usually the identifier is returned by the server.



POST /resources HTTP/1.1



Update:



Can only be performed with PUT in the following way:




PUT



Updates the resource with existingResourceId as the identifier, under the /resources URI, or collection.



PUT /resources/<existingResourceId> HTTP/1.1



Explanation:



When dealing with REST and URI as general, you have generic on the left and specific on the right. The generics are usually called collections and the more specific items can be called resource. Note that a resource can contain a collection.




Examples:



<-- generic -- specific -->



URI: website.com/users/john
website.com - whole site
users - collection of users
john - item of the collection, or a resource

URI:website.com/users/john/posts/23
website.com - whole site
users - collection of users
john - item of the collection, or a resource
posts - collection of posts from john
23 - post from john with identifier 23, also a resource



When you use POST you are always refering to a collection, so whenever you say:



POST /users HTTP/1.1


you are posting a new user to the users collection.



If you go on and try something like this:



POST /users/john HTTP/1.1


it will work, but semantically you are saying that you want to add a resource to the john collection under the users collection.



Once you are using PUT you are refering to a resource or single item, possibly inside a collection. So when you say:



PUT /users/john HTTP/1.1


you are telling to the server update, or create if it doesn't exist, the john resource under the users collection.



Spec:



Let me highlight some important parts of the spec:



POST




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




Hence, creates a new resource on a collection.



PUT




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."




Hence, create or update based on existence of the resource.



Reference:




  • HTTP/1.1 Spec

  • Wikipedia - REST

  • Uniform Resource Identifiers (URI): Generic Syntax and Semantics






share|improve this answer





















  • 7





    This post was helpful to me in understanding that POST adds "something" as a child to the given collection (URI), whereas PUT explicitly defines the "something" at the given URI location.

    – kwah
    Nov 23 '13 at 16:33






  • 2





    This is the best answer, here, I think: none of this "POST can update a resource" nonsense. I like your statement, "Update can only be performed with PUT".

    – Thomas
    May 28 '15 at 13:44






  • 2





    No, PUT is not for update or create. It is for replacing. Note that you can replace nothing with something for the effect of creating.

    – thecoshman
    Jun 8 '15 at 8:07






  • 2





    @7hi4g0 PUT is for for updating with a complete replacement, in other words, it replaces. You replace nothing with something, or something with a completely new something. PUT is not for making a minor change (unless you have the client make that minor change and provide the entire new version, even what is remaining the same). For partial modification, PATCH is the method of choice.

    – thecoshman
    Jun 8 '15 at 12:57






  • 1





    @thecoshman You could, but it wouldn't be too clear that create is also covered in there. In this case, it is better to be explicit.

    – 7hi4g0
    Jun 9 '15 at 20:21



















163














I'd like to add my "pragmatic" advice. Use PUT when you know the "id" by which the object you are saving can be retrieved. Using PUT won't work too well if you need, say, a database generated id to be returned for you to do future lookups or updates.



So: To save an existing user, or one where the client generates the id and it's been verified that the id is unique:



PUT /user/12345 HTTP/1.1  <-- create the user providing the id 12345
Host: mydomain.com

GET /user/12345 HTTP/1.1 <-- return that user
Host: mydomain.com


Otherwise, use POST to initially create the object, and PUT to update the object:



POST /user HTTP/1.1   <--- create the user, server returns 12345
Host: mydomain.com

PUT /user/12345 HTTP/1.1 <--- update the user
Host: mydomain.com





share|improve this answer





















  • 14





    Actually, it should be POST /users. (Note that /users is plural.) This has the affect of creating a new user and making it a child resource of the /users collection.

    – DavidRR
    Dec 16 '14 at 13:54








  • 5





    @DavidRR to be fair, how to handle groups is another debate altogether. GET /users makes sense, it reads as you want, but I'd be ok with GET /user/<id> or POST /user (with payload for said new user) because it reads correctly 'get me users 5' is odd, but 'get me user 5' is more natural. I'd probably still fall down on the side of pluralisation though :)

    – thecoshman
    Jun 8 '15 at 7:57



















155














POST means "create new" as in "Here is the input for creating a user, create it for me".



PUT means "insert, replace if already exists" as in "Here is the data for user 5".



You POST to example.com/users since you don't know the URL of the user yet, you want the server to create it.



You PUT to example.com/users/id since you want to replace/create a specific user.



POSTing twice with the same data means create two identical users with different ids. PUTing twice with the same data creates the user the first and updates him to the same state the second time (no changes). Since you end up with the same state after a PUT no matter how many times you perform it, it is said to be "equally potent" every time - idempotent. This is useful for automatically retrying requests. No more 'are you sure you want to resend' when you push the back button on the browser.



A general advice is to use POST when you need the server to be in control of URL generation of your resources. Use PUT otherwise. Prefer PUT over POST.






share|improve this answer





















  • 8





    Sloppiness may have cause it to be commonly taught that there are only two verbs you need: GET and POST. GET to obtain, POST to change. Even PUT and DELETE were performed using POST. Asking what PUT really means 25 years later maybe a sign we learned it wrong at first. REST popularity drove people back to the basics where we must now unlearn past bad mistakes. POST was overused and now commonly taught incorrectly. Best part: "POSTing twice with the same data means create two identical [resources]". Great point!

    – maxpolk
    Sep 1 '14 at 19:36








  • 1





    How can you use PUT to create a record by the ID, like in your example user 5 if it doesn't exist yet? Don't you mean update, replace if already exists? or something

    – Luke
    Nov 28 '14 at 12:44













  • @Coulton: I meant what I wrote. You insert user 5 if you PUT to /users/5 and #5 does not exist yet.

    – Alexander Torstling
    Nov 28 '14 at 15:46











  • @Coulton: And PUT can also be used to replace the value of an existing resource in its entirety.

    – DavidRR
    Dec 16 '14 at 14:02








  • 1





    "Prefer PUT over POST"... care to justify that?

    – thecoshman
    Jun 8 '15 at 8:04



















117














Use POST to create, and PUT to update. That's how Ruby on Rails is doing it, anyway.



PUT    /items/1      #=> update
POST /items #=> create





share|improve this answer





















  • 4





    POST /items adds a new item to an already defined resource ('item'). It does not, as the answer says, "create a group." I don't understand why this has 12 votes.

    – David J.
    Jun 21 '12 at 5:26













  • Out of the box, Rails does not support 'creating a group' via REST. To 'create a group' by which I mean 'create a resource' you have to do it via the source code.

    – David J.
    Jun 21 '12 at 5:28






  • 8





    This is a fair guideline, but an oversimplification. As the other answers mention, either method could be used for both create and update.

    – Brad Koch
    Mar 7 '13 at 15:55






  • 2





    I agree with the answer with a slight modification. Use POST to create and PUT to update the resource completely. For partial updates, we can use PUT or PATCH. Lets say we want to update the status of a group. We can use PUT /groups/1/status with the status is the request payload or PATCH /groups/1 with the details about the action in the payload

    – java_geek
    Oct 6 '14 at 6:26






  • 2





    It should also be made clear that PUT /items/42 is also valid for creating a resource, but only if the client has the privilege of naming the resource. (Does Rails allow a client this naming privilege?)

    – DavidRR
    Dec 16 '14 at 14:10





















70














Both are used for data transmission between client to server, but there are subtle differences between them, which are:



Enter image description here



Analogy:




  • PUT i.e. take and put where it was.

  • POST as send mail in post office.


enter image description here






share|improve this answer





















  • 5





    So in summary: POST for inserts, PUT for updates

    – MobileMon
    Sep 22 '15 at 12:31






  • 14





    @MobileMon No, REST methods are not CRUD.

    – jlr
    Jan 8 '16 at 19:06











  • I'd say PUT for UPSERTS

    – Hola Soy Edu Feliz Navidad
    Nov 26 '18 at 11:45



















63














REST is a very high-level concept. In fact, it doesn't even mention HTTP at all!



If you have any doubts about how to implement REST in HTTP, you can always take a look at the Atom Publication Protocol (AtomPub) specification. AtomPub is a standard for writing RESTful webservices with HTTP that was developed by many HTTP and REST luminaries, with some input from Roy Fielding, the inventor of REST and (co-)inventor of HTTP himself.



In fact, you might even be able to use AtomPub directly. While it came out of the blogging community, it is in no way restricted to blogging: it is a generic protocol for RESTfully interacting with arbitrary (nested) collections of arbitrary resources via HTTP. If you can represent your application as a nested collection of resources, then you can just use AtomPub and not worry about whether to use PUT or POST, what HTTP Status Codes to return and all those details.



This is what AtomPub has to say about resource creation (section 9.2):




To add members to a Collection, clients send POST requests to the URI of the Collection.







share|improve this answer





















  • 7





    There's nothing wrong with allowing PUT to create resources. Just be aware that it means that the client provides the URL.

    – Julian Reschke
    Apr 7 '10 at 7:47






  • 5





    There's something very wrong with allowing PUT to create resources: the client provides the URL. That's the server's job!

    – Joshcodes
    Oct 29 '13 at 17:33











  • @Joshcodes It is not always the case that it is the server's job to create client ids. I have increasingly seen designs that let clients generate some sort of UUID as the resource id. This design lends itself in particular to increase scale.

    – Justin Ohms
    Feb 5 '17 at 18:26











  • @JustinOhms I agree with your point about client generated IDs (side note: all systems designed by me since circa 2008 require the client to create the ID as a UUID/Guid). That does not mean the client should specify the URL.

    – Joshcodes
    Feb 6 '17 at 18:24






  • 1





    Yes, if the resource already exists, use PUT. However, in nearly all cases, the resources should be created with POST and the client should not provide the URL. Roy Fielding agrees with this statement FWIW: roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven

    – Joshcodes
    Feb 7 '17 at 1:26



















56














The decision of whether to use PUT or POST to create a resource on a server with an HTTP + REST API is based on who owns the URL structure. Having the client know, or participate in defining, the URL struct is an unnecessary coupling akin to the undesirable couplings that arose from SOA. Escaping types of couplings is the reason REST is so popular. Therefore, the proper method to use is POST. There are exceptions to this rule and they occur when the client wishes to retain control over the location structure of the resources it deploys. This is rare and likely means something else is wrong.



At this point some people will argue that if RESTful-URL's are used, the client does knows the URL of the resource and therefore a PUT is acceptable. After all, this is why canonical, normalized, Ruby on Rails, Django URLs are important, look at the Twitter API … blah blah blah. Those people need to understand there is no such thing as a Restful-URL and that Roy Fielding himself states that:




A REST API must not define fixed resource names or hierarchies (an
obvious coupling of client and server). Servers must have the freedom
to control their own namespace. Instead, allow servers to instruct
clients on how to construct appropriate URIs, such as is done in HTML
forms and URI templates, by defining those instructions within media
types and link relations. [Failure here implies that clients are
assuming a resource structure due to out-of band information, such as
a domain-specific standard, which is the data-oriented equivalent to
RPC's functional coupling].



http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven




The idea of a RESTful-URL is actually a violation of REST as the server is in charge of the URL structure and should be free to decide how to use it to avoid coupling. If this confuses you read about the significance of self discovery on API design.



Using POST to create resources comes with a design consideration because POST is not idempotent. This means that repeating a POST several times does not guarantee the same behavior each time. This scares people into using PUT to create resources when they should not. They know it's wrong (POST is for CREATE) but they do it anyway because they don't know how to solve this problem. This concern is demonstrated in the following situation:




  1. The client POST a new resource to the server.

  2. The server processes the request and sends a response.

  3. The client never receives the response.

  4. The server is unaware the client has not received the response.

  5. The client does not have a URL for the resource (therefore PUT is not an option) and repeats the POST.

  6. POST is not idempotent and the server …


Step 6 is where people commonly get confused about what to do. However, there is no reason to create a kludge to solve this issue. Instead, HTTP can be used as specified in RFC 2616 and the server replies:




10.4.10 409 Conflict



The request could not be completed due to a conflict with the current
state of the resource. This code is only allowed in situations where
it is expected that the user might be able to resolve the conflict and
resubmit the request. The response body SHOULD include enough



information for the user to recognize the source of the conflict.
Ideally, the response entity would include enough information for the
user or user agent to fix the problem; however, that might not be
possible and is not required.



Conflicts are most likely to occur in response to a PUT request. For
example, if versioning were being used and the entity being PUT
included changes to a resource which conflict with those made by an
earlier (third-party) request, the server might use the 409 response
to indicate that it can’t complete the request. In this case, the
response entity would likely contain a list of the differences between
the two versions in a format defined by the response Content-Type.




Replying with a status code of 409 Conflict is the correct recourse because:




  • Performing a POST of data which has an ID which matches a resource already in the system is “a conflict with the current state of the resource.”

  • Since the important part is for the client to understand the server has the resource and to take appropriate action. This is a “situation(s) where it is expected that the user might be able to resolve the conflict and resubmit the request.”

  • A response which contains the URL of the resource with the conflicting ID and the appropriate preconditions for the resource would provide “enough information for the user or user agent to fix the problem” which is the ideal case per RFC 2616.


Update based on release of RFC 7231 to Replace 2616



RFC 7231 is designed to replace 2616 and in Section 4.3.3 describes the follow possible response for a POST




If the result of processing a POST would be equivalent to a
representation of an existing resource, an origin server MAY redirect
the user agent to that resource by sending a 303 (See Other) response
with the existing resource's identifier in the Location field. This
has the benefits of providing the user agent a resource identifier
and transferring the representation via a method more amenable to
shared caching, though at the cost of an extra request if the user
agent does not already have the representation cached.




It now may be tempting to simply return a 303 in the event that a POST is repeated. However, the opposite is true. Returning a 303 would only make sense if multiple create requests (creating different resources) return the same content. An example would be a "thank you for submitting your request message" that the client need not re-download each time. RFC 7231 still maintains in section 4.2.2 that POST is not to be idempotent and continues to maintain that POST should be used for create.



For more information about this, read this article.






share|improve this answer


























  • Would a 409 Conflict response be the appropriate code for something like trying to create a new account with a username that already exists? I've been using 409 for versioning conflicts specifically, but after reading your answer, I wonder if it shouldn't be used for any "duplicate" requests.

    – Eric B.
    Jul 9 '14 at 4:43











  • @EricB. Yes, in the situation you describe "due to a conflict with the current state of the resource" the operation fails. Additionally, it is reasonable to expect that the user can resolve the conflict and the message body only needs to inform the user that the username already exists.

    – Joshcodes
    Jul 10 '14 at 13:25











  • @Joshcodes can you say more about the conflict resolution process? In this case, if the username already exists is the client expected to prompt the end user for a different username? What if the client is actually trying to use POST to change the username? Should PUT requests still be used for updating parameters, while POST is used for creating objects whether it be one at a time or several? Thanks.

    – BFar
    Jan 22 '15 at 22:41











  • @BFar2 if the username already exists then the client should prompt the user. To change the username, assuming the username is part of an already created resource which needs modified, PUT would be used because you are correct, POST is used for create, always and PUT for updates.

    – Joshcodes
    Jan 23 '15 at 1:09











  • explaining things using short and effective language is also a desirable skill

    – Junchen Liu
    Aug 24 '15 at 16:27



















51














I like this advice, from RFC 2616's definition of 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.




This jibes with the other advice here, that PUT is best applied to resources that already have a name, and POST is good for creating a new object under an existing resource (and letting the server name it).



I interpret this, and the idempotency requirements on PUT, to mean that:




  • POST is good for creating new objects under a collection (and create does not need to be idempotent)

  • PUT is good for updating existing objects (and update needs to be idempotent)

  • POST can also be used for non-idempotent updates to existing objects (especially, changing part of an object without specifying the whole thing -- if you think about it, creating a new member of a collection is actually a special case of this kind of update, from the collection's perspective)

  • PUT can also be used for create if and only if you allow the client to name the resource. But since REST clients aren't supposed to make assumptions about URL structure, this is less in the intended spirit of things.






share|improve this answer



















  • 3





    "POST can also be used for non-idempotent updates to existing objects (especially, changing part of an object without specifying the whole thing" That's what PATCH is for

    – Snuggs
    May 4 '12 at 22:11





















43














In short:



PUT is idempotent, where the resource state will be the same if the same operation is executed one time or multiple times.



POST is non-idempotent, where the resource state may become different if the operation is executed multiple times as compared to executing a single time.



Analogy with database query



PUT You can think of similar to "UPDATE STUDENT SET address = "abc" where id="123";



POST You can think of something like "INSERT INTO STUDENT(name, address) VALUES ("abc", "xyzzz");



Student Id is auto generated.



With PUT, if the same query is executed multiple times or one time, the STUDENT table state remains the same.



In case of POST, if the same query is executed multiple times then multiple Student records get created in the database and the database state changes on each execution of an "INSERT" query.



NOTE: PUT needs a resource location (already-resource) on which update needs to happen, whereas POST doesn't require that. Therefore intuitively POST is meant for creation of a new resource, whereas PUT is needed for updating the already existing resource.



Some may come up with that updates can be performed with POST. There is no hard rule which one to use for updates or which one to use for create. Again these are conventions, and intuitively I'm inclined with the above mentioned reasoning and follow it.






share|improve this answer





















  • 5





    for PUT is similar to INSERT or UPDATE query

    – Eugen Konkov
    Aug 22 '16 at 9:25






  • 1





    actually PUT You can think of similar to "UPDATE STUDENT SET address = "abc" where id="123"; would be a statement for PATCH. "UPDATE STUDENT SET address = "abc", name="newname" where id="123" would be a correct analogy for PUT

    – mko
    Apr 12 '17 at 13:24











  • Put could also be used for INSERT. For example if you server detected you were trying to upload the same file multiple times, it would make your request idempotent. (No new file uploads are done).

    – kiwicomb123
    Aug 20 '18 at 22:46



















41














POST is like posting a letter to a mailbox or posting an email to an email queue.
PUT is like when you put an object in a cubby hole or a place on a shelf (it has a known address).



With POST, you're posting to the address of the QUEUE or COLLECTION. With PUT, you're putting to the address of the ITEM.



PUT is idempotent. You can send the request 100 times and it will not matter. POST is not idempotent. If you send the request 100 times, you'll get 100 emails or 100 letters in your postal box.



A general rule: if you know the id or name of the item, use PUT. If you want the id or name of the item to be assigned by the receiving party, use POST.



POST versus PUT






share|improve this answer





















  • 1





    No, PUT implies that you know the URL. If you only know the ID then POST with that ID to get the URL.

    – Joshcodes
    Oct 29 '13 at 17:35






  • 5





    The id is part of the URL, so yes, use PUT if you know the URL (which includes the id).

    – Homer6
    Oct 29 '13 at 22:02











  • No, the URL is determined by the server and the ID is not necessarily part of the URL. Roy Fielding would tell you the same or you could just read his thesis.

    – Joshcodes
    Oct 29 '13 at 23:06











  • @Joshcodes, is that assuming REST? In a RESTful architecture, the item id is most definitely part of the URL, as in: /people/123. I like this site for REST: microformats.org/wiki/rest/urls

    – Beez
    Dec 26 '13 at 19:10






  • 1





    @Beez the mircoformats link suggests a good way for servers to structure their URLs but the server determines the URL. The client next-to-never does. See my answer or associated article if you don't understand this.

    – Joshcodes
    Jan 7 '14 at 17:11





















37














New answer (now that I understand REST better):



PUT is merely a statement of what content the service should, from now on, use to render representations of the resource identified by the client; POST is a statement of what content the service should, from now on, contain (possibly duplicated) but it's up to the server how to identify that content.



PUT x (if x identifies a resource): "Replace the content of the resource identified by x with my content."



PUT x (if x does not identify a resource): "Create a new resource containing my content and use x to identify it."



POST x: "Store my content and give me an identifier that I can use to identify a resource (old or new) containing said content (possibly mixed with other content). Said resource should be identical or subordinate to that which x identifies." "y's resource is subordinate to x's resource" is typically but not necessarily implemented by making y a subpath of x (e.g. x = /foo and y = /foo/bar) and modifying the representation(s) of x's resource to reflect the existence of a new resource, e.g. with a hyperlink to y's resource and some metadata. Only the latter is really essential to good design, as URLs are opaque in REST -- you're supposed to use hypermedia instead of client-side URL construction to traverse the service anyways.



In REST, there's no such thing as a resource containing "content". I refer as "content" to data that the service uses to render representations consistently. It typically consists of some related rows in a database or a file (e.g. an image file). It's up to the service to convert the user's content into something the service can use, e.g. converting a JSON payload into SQL statements.



Original answer (might be easier to read):



PUT /something (if /something already exists): "Take whatever you have at /something and replace it with what I give you."



PUT /something (if /something does not already exist): "Take what I give you and put it at /something."



POST /something: "Take what I give you and put it anywhere you want under /something as long as you give me its URL when you're done."






share|improve this answer


























  • But how can you use PUT to create a new resource if it doesn't exist, while your ID generation method is on Auto Increment ? Usually ORM's does auto generate the ID for you, like the way you want it to be in a POST for example. Does it mean that if you want to implement PUT the right way you have to change your id auto generation ? This is awkward if the answer is yes.

    – Roni Axelrad
    Sep 16 '18 at 15:28






  • 1





    @RoniAxelrad : PUT is like a database "INSERT OR UPDATE" statement where you are including the key in the statement, so only applicable where you can guarente no collisions. eg. your domain has a 'natural key' or you use a guid. POST is like inserting into a table with an auto incrementing key. You have to be told by the database what ID it got after it has been inserted. Note your "INSERT OR UPDATE" will replace any previous data if it existed.

    – Nigel Thorne
    Nov 26 '18 at 1:33













  • @NigelThorne Thanks for your answer. So if for example I'm trying to PUT a book id 10 with a URI: PUT books/10. If book id 10 does not exists, I should create a book with id 10 right? but I cannot control the creation ID numerator, because it's auto increment. what should I do in that situation ?

    – Roni Axelrad
    Nov 27 '18 at 20:50






  • 1





    @RoniAxelrad REST PUT to an ID that doesn't exist is a request to the server to create a resource. It's still up to the server to decide if it wants to allow that. The server is in charge. It can respond with "No. I'm not going to do that". You already do that if the user doesn't have enough permissions...etc. It's okay for the server to say "No". REST is a convention that lets us define the meaning of various types of request ... your server decides what to do with those requests based on your business logic :) Even if it says "no" it's still following REST :)

    – Nigel Thorne
    Dec 3 '18 at 7:03





















34














Ruby on Rails 4.0 will use the 'PATCH' method instead of PUT to do partial updates.



RFC 5789 says about PATCH (since 1995):




A new method is necessary to improve interoperability and prevent
errors. The PUT method is already defined to overwrite a resource
with a complete new body, and cannot be reused to do partial changes.
Otherwise, proxies and caches, and even clients and servers, may get
confused as to the result of the operation. POST is already used but
without broad interoperability (for one, there is no standard way to
discover patch format support). PATCH was mentioned in earlier HTTP
specifications, but not completely defined.




"Edge Rails: PATCH is the new primary HTTP method for updates" explains it.






share|improve this answer

































    33














    Short Answer:



    Simple rule of thumb: Use POST to create, use PUT to update.



    Long Answer:



    POST:




    • POST is used to send data to server.

    • Useful when the resource's URL is
      unknown


    PUT:




    • PUT is used to transfer state to the server

    • Useful when a resource's URL is known


    Longer Answer:



    To understand it we need to question why PUT was required, what were the problems PUT was trying to solve that POST couldn't.



    From a REST architecture's point of view there is none that matters. We could have lived without PUT as well. But from a client developer's point of view it made his/her life a lot simpler.



    Prior to PUT, clients couldn't directly know the URL that the server generated or if all it had generated any or whether the data to be sent to the server is already updated or not. PUT relieved the developer of all these headaches. PUT is idempotent, PUT handles race conditions, and PUT lets the client choose the URL.






    share|improve this answer





















    • 3





      Your short answer might be VERY wrong. HTTP PUT is free to be repeated by HTTP proxies. And so, if PUT is actually doing SQL INSERT it might fail second time, which means it would return different result and so it would not be IDEMPOTENT (which is the difference between PUT and POST)

      – Kamil Tomšík
      Apr 30 '18 at 17:27



















    26














    At the risk of restating what has already been said, it seems important to remember that PUT implies that the client controls what the URL is going to end up being, when creating a resource. So part of the choice between PUT and POST is going to be about how much you can trust the client to provide correct, normalized URL that are coherent with whatever your URL scheme is.



    When you can't fully trust the client to do the right thing, it would be
    more appropriate to use POST to create a new item and then send the URL back to the client in the response.






    share|improve this answer





















    • 2





      I'm a bit late to this - but someone saying something similar on another website got it to click for me. If you're creating a resource and using an auto-incremented ID as it's "identifier" instead of a user assigned name, it should be a POST.

      – Ixmatus
      Feb 3 '12 at 18:51






    • 2





      This isn't quite right - PUT can still create a resource by referring to it with a non-canonical name, as long as in the response, the server returns a Location header that does contain the canonical resource name.

      – Ether
      Oct 19 '12 at 16:08






    • 1





      @Joshcodes don't forget that you can have many URIs referencing the same underlying resource. So what Ether said is sound advice, the client can PUT to a URL (that might be more semantic, like PUT /X-files/series/4/episodes/max) and the server respond with a URI that provides a short canonical unique link to that new resource (ie /X-Ffiles/episodes/91)

      – thecoshman
      Jun 8 '15 at 8:02











    • @thecoshman the issue is the concern for the URL structure does not belong to the client. Reading about self-discovery (also part of REST) may help make this clear.

      – Joshcodes
      Jun 8 '15 at 17:50











    • @Joshcodes then by that logic, a client should never use PUT to create as they shouldn't be concerned with with providing the URL. Well... unless the server provided a URL to PUT to if the client wants to put to it... something like "PUT /comments/new" and the server might respond "204 /comments/234532" but that seems a bit RPC to me, the client should just POST to /comments...

      – thecoshman
      Jun 9 '15 at 16:18



















    20














    The most important consideration is reliability. If a POST message gets lost the state of the system is undefined. Automatic recovery is impossible. For PUT messages, the state is undefined only until the first successful retry.



    For instance, it may not be a good idea to create credit card transactions with POST.



    If you happen to have auto generated URI's on your resource you can still use PUT by passing a generated URI (pointing to an empty resource) to the client.



    Some other considerations:




    • POST invalidates cached copies of the entire containing resource (better consistency)

    • PUT responses are not cacheable while POST ones are (Require Content-Location and expiration)

    • PUT is less supported by e.g. Java ME, older browsers, firewalls






    share|improve this answer


























    • This is incorrect. For POST, the state is also undefined only until the first successful retry. Then, either the server accepts the POST (message never arrived), throws a 409 conflict for a duplicate ID (message arrived, response was lost), or any other valid response.

      – Joshcodes
      Apr 24 '14 at 12:13











    • In general a useragent would not able to safely retry the POST operation since the POST operation gives no that guarantee that two operations would have the same effect as one. The term "ID" has nothing to do with HTTP. The URI identifies the resource.

      – Hans Malherbe
      Jul 25 '14 at 5:48











    • A useragent can "safely" retry a POST operation as many times as it wants. It will just receive a duplicate ID error (assuming the resource has an ID) or a duplicate data error (assuming that's an issue and the resource does not have IDs).

      – Joshcodes
      Jul 27 '14 at 2:10











    • Bangs head against wall. HTTP has no solution to the problem of reliability, and this is not well understood, not much discussed, and simply not catered for in the vast majority of web applications. @Joshcodes I have an answer to this question. I essentially agree with Hans. There's a problem.

      – bbsimonbb
      Jun 13 '18 at 8:21











    • @bbsimonbb, HTTP has a robust and well documented set of error responses. My answer to this question (stackoverflow.com/questions/630453/put-vs-post-in-rest/…) covers how to use http according to specification to achieve consistency.

      – Joshcodes
      Jun 19 '18 at 12:50



















    20














    In a very simple way I'm taking the example of the Facebook timeline.



    Case 1: When you post something on your timeline, it's a fresh new entry. So in this case they use the POST method because the POST method is non-idempotent.



    Case 2: If your friend comment on your post the first time, that also will create a new entry in the database so the POST method used.



    Case 3: If your friend edits his comment, in this case, they had a comment id, so they will update an existing comment instead of creating a new entry in the database. Therefore for this type of operation use the PUT method because it is idempotent.*



    In a single line, use POST to add a new entry in the database and PUT to update something in the database.






    share|improve this answer





















    • 4





      If the comment is an object with property like user id, created date, comment-message, etc. and at the time of edit only comment-message is getting updated, PATCH should be done here?

      – Habeeb Perwad
      Aug 12 '17 at 10:47











    • PUT is used by FB to update the comment because an existing resource is being updated, and that is what PUT does (updates a resource). PUT happens to be idempotent, in contrast to POST. An HTTP verb being idempotent affects the error handling but does not dictate usage. See my answer for a more detail explanation: stackoverflow.com/questions/630453/put-vs-post-in-rest/…

      – Joshcodes
      Jun 19 '18 at 12:57



















    14














    There seems to always be some confusion as to when to use the HTTP POST versus the HTTP PUT method for REST services. Most developers will try to associate CRUD operations directly to HTTP methods. I will argue that this is not correct and one can not simply associate the CRUD concepts to the HTTP methods. That is:



    Create => HTTP PUT
    Retrieve => HTTP GET
    Update => HTTP POST
    Delete => HTTP DELETE


    It is true that the R(etrieve) and D(elete) of the CRUD operations can be mapped directly to the HTTP methods GET and DELETE respectively. However, the confusion lies in the C(reate) and U(update) operations. In some cases, one can use the PUT for a create while in other cases a POST will be required. The ambiguity lies in the definition of an HTTP PUT method versus an HTTP POST method.



    According to the HTTP 1.1 specifications the GET, HEAD, DELETE, and PUT methods must be idempotent, and the POST method is not idempotent. That is to say that an operation is idempotent if it can be performed on a resource once or many times and always return the same state of that resource. Whereas a non idempotent operation can return a modified state of the resource from one request to another. Hence, in a non idempotent operation, there is no guarantee that one will receive the same state of a resource.



    Based on the above idempotent definition, my take on using the HTTP PUT method versus using the HTTP POST method for REST services is:
    Use the HTTP PUT method when:



    The client includes all aspect of the resource including the unique identifier to uniquely identify the resource. Example: creating a new employee.
    The client provides all the information for a resource to be able to modify that resource.This implies that the server side does not update any aspect of the resource (such as an update date).


    In both cases, these operations can be performed multiple times with the same results. That is the resource will not be changed by requesting the operation more than once. Hence, a true idempotent operation.
    Use the HTTP POST method when:



    The server will provide some information concerning the newly created resource. For example, take a logging system. A new entry in the log will most likely have a numbering scheme which is determined on the server side. Upon creating a new log entry, the new sequence number will be determined by the server and not by the client.
    On a modification of a resource, the server will provide such information as a resource state or an update date. Again in this case not all information was provided by the client and the resource will be changing from one modification request to the next. Hence a non idempotent operation.


    Conclusion



    Do not directly correlate and map CRUD operations to HTTP methods for REST services. The use of an HTTP PUT method versus an HTTP POST method should be based on the idempotent aspect of that operation. That is, if the operation is idempotent, then use the HTTP PUT method. If the operation is non idempotent, then use the HTTP POST method.






    share|improve this answer



















    • 2





      Update => HTTP POST : POST is not for updating

      – Premraj
      Jan 30 '16 at 0:57





















    13















    the origin server can create the resource with that URI




    So you use POST and probably, but not necessary PUT for resource creation. You don't have to support both. For me POST is perfectly enough. So it is a design decision.



    As your quote mentioned, you use PUT for creation of there is no resource assigned to an IRI, and you want to create a resource anyway. For example, PUT /users/123/password usually replaces the old password with a new one, but you can use it to create a password if it does not exist already (for example, by freshly registered users or by restoring banned users).






    share|improve this answer


























    • I think you've managed to provide one of the few good examples of how to use PUT, well done.

      – thecoshman
      Jun 8 '15 at 8:13



















    13














    Readers new to this topic will be struck by the endless discussion about what you should do, and the relative absence of lessons from experience. The fact that REST is "preferred" over SOAP is, I suppose, a high-level learning from experience, but goodness we must have progressed from there? It's 2016. Roy's dissertation was in 2000. What have we developed? Was it fun? Was it easy to integrate with? To support? Will it handle the rise of smartphones and flaky mobile connections?



    According to ME, real-life networks are unreliable. Requests timeout. Connections are reset. Networks go down for hours or days at a time. Trains go into tunnels with mobile users aboard. For any given request (as occasionally acknowledged in all this discussion) the request can fall in the water on its way, or the response can fall in the water on its way back. In these conditions, issuing PUT, POST and DELETE requests directly against substantive resources has always struck me as a little brutal and naive.



    HTTP does nothing to ensure reliable completion of the request-response, and that's just fine because this is properly the job of network-aware applications. Developing such an application, you can jump through hoops to use PUT instead of POST, then more hoops to give a certain kind of error on the server if you detect duplicate requests. Back at the client, you then have to jump through hoops to interpret these errors, refetch, revalidate and repost.



    Or you can do this: consider your unsafe requests as ephemeral single-user resources (let's call them actions). Clients request a new "action" on a substantive resource with an empty POST to the resource. POST will be used only for this. Once safely in possession of the URI of the freshly minted action, the client PUTs the unsafe request to the action URI, not the target resource. Resolving the action and updating the "real" resource is properly the job of your API, and is here decoupled from the unreliable network.



    The server does the business, returns the response and stores it against the agreed action URI. If anything goes wrong, the client repeats the request (natural behaviour!), and if the server has already seen it, it repeats the stored response and does nothing else.



    You will quickly spot the similarity with promises: we create and return the placeholder for the result before doing anything. Also like a promise, an action can succeed or fail one time, but its result can be fetched repeatedly.



    Best of all, we give sending and receiving applications a chance to link the uniquely identified action to uniqueness in their respective environments. And we can start to demand, and enforce!, responsible behaviour from clients: repeat your requests as much as you like, but don't go generating a new action until you're in possession of a definitive result from the existing one.



    As such, numerous thorny problems go away. Repeated insert requests won't create duplicates, and we don't create the real resource until we're in possession of the data. (database columns can stay not-nullable). Repeated update requests won't hit incompatible states and won't overwrite subsequent changes. Clients can (re)fetch and seamlessy process the original confirmation for whatever reason (client crashed, response went missing, etc.).



    Successive delete requests can see and process the original confirmation, without hitting a 404 error. If things take longer than expected, we can respond provisionally, and we have a place where the client can check back for the definitive result. The nicest part of this pattern is its Kung-Fu (Panda) property. We take a weakness, the propensity for clients to repeat a request any time they don't understand the response, and turn it into a strength :-)



    Before telling me this is not RESTful, please consider the numerous ways in which REST principles are respected. Clients don't construct URLs. The API stays discoverable, albeit with a little change in semantics. HTTP verbs are used appropriately. If you think this is a huge change to implement, I can tell you from experience that it's not.



    If you think you'll have huge amounts of data to store, let's talk volumes: a typical update confirmation is a fraction of a kilobyte. HTTP currently gives you a minute or two to respond definitively. Even if you only store actions for a week, clients have ample chance to catch up. If you have very high volumes, you may want a dedicated acid-compliant key value store, or an in-memory solution.






    share|improve this answer


























    • Wont storing response be like maintaining a session? Which would cause (horizontal) scaling issues.

      – Saurabh Harwande
      Aug 21 '18 at 17:57





















    11














    I'm going to land with the following:



    PUT refers to a resource, identified by the URI. In this case, you are updating it. It is the part of the three verbs referring to resources -- delete and get being the other two.



    POST is basically a free form message, with its meaning being defined 'out of band'. If the message can be interpreted as adding a resource to a directory, that would be OK, but basically you need to understand the message you are sending (posting) to know what will happen with the resource.





    Because PUT and GET and DELETE refer to a resource, they are also by definition idempotent.



    POST can perform the other three functions, but then the semantics of the request will be lost on the intermediaries such as caches and proxies. This also applies to providing security on the resource, since a post's URI doesn't necessarily indicate the resource it is applying to (it can though).



    A PUT doesn't need to be a create; the service could error if the resource isn't already created, but otherwise update it. Or vice versa -- it may create the resource, but not allow updates. The only thing required about PUT is that it points to a specific resource, and its payload is the representation of that resource. A successful PUT means (barring interference) that a GET would retrieve the same resource.





    Edit: One more thing -- a PUT can create, but if it does then the ID has to be a natural ID -- AKA an email address. That way when you PUT twice, the second put is an update of the first. This makes it idempotent.



    If the ID is generated (a new employee ID, for example), then the second PUT with the same URL would create a new record, which violates the idempotent rule. In this case the verb would be POST, and the message (not resource) would be to create a resource using the values defined in this message.






    share|improve this answer

































      9














      The semantics are supposed be different, in that "PUT", like "GET" is supposed to be idempotent -- meaning, you can the same exact PUT request multiple times and the result will be as if you executed it only once.



      I will describe the conventions which I think are most widely used and are most useful:



      When you PUT a resource at a particular URL what happens is that it should get saved at that URL, or something along those lines.



      When you POST to a resource at a particular URL, often you are posting a related piece of information to that URL. This implies that the resource at the URL already exists.



      For example, when you want to create a new stream, you can PUT it to some URL. But when you want to POST a message to an existing stream, you POST to its URL.



      As for modifying the properties of the stream, you can do that with either PUT or POST. Basically, only use "PUT" when the operation is idempotent - otherwise use POST.



      Note, however, that not all modern browsers support HTTP verbs other than GET or POST.






      share|improve this answer
























      • What you describe POST as is actually how PATCH should behave. POST is supposed to mean something more akin to "append" as in "post to mailing list".

        – Alexander Torstling
        Nov 28 '14 at 15:57



















      7














      Most of the time, you will use them like this:





      • POST a resource into a collection


      • PUT a resource identified by collection/:id


      For example:





      • POST /items


      • PUT /items/1234


      In both cases, the request body contains the data for the resource to be created or updated. It should be obvious from the route names that POST is not idempotent (if you call it 3 times it will create 3 objects), but PUT is idempotent (if you call it 3 times the result is the same). PUT is often used for "upsert" operation (create or update), but you can always return a 404 error if you only want to use it to modify.



      Note that POST "creates" a new element in the collection, and PUT "replaces" an element at a given URL, but it is a very common practice to use PUT for partial modifications, that is, use it only to update existing resources and only modify the included fields in the body (ignoring the other fields). This is technically incorrect, if you want to be REST-purist, PUT should replace the whole resource and you should use PATCH for the partial update. I personally don't care much as far as the behavior is clear and consistent across all your API endpoints.



      Remember, REST is a set of conventions and guidelines to keep your API simple. If you end up with a complicated work-around just to check the "RESTfull" box then you are defeating the purpose ;)






      share|improve this answer































        7














        While there is probably an agnostic way to describe these, it does seem to be conflicting with various statements from answers to websites.



        Let's be very clear and direct here. If you are a .NET developer working with Web API, the facts are (from the Microsoft API documentation),
        http://www.asp.net/web-api/overview/creating-web-apis/creating-a-web-api-that-supports-crud-operations:



        1. PUT = UPDATE (/api/products/id)
        2. MCSD Exams 2014 - UPDATE = PUT, there are **NO** multiple answers for that question period.


        Sure you "can" use "POST" to update, but just follow the conventions laid out for you with your given framework. In my case it is .NET / Web API, so PUT is for UPDATE there is no debate.



        I hope this helps any Microsoft developers that read all comments with Amazon and Sun/Java website links.






        share|improve this answer

































          7














          In addition to differences suggested by others, I want to add one more.



          In POST method you can send body params in form-data



          In PUT method you have to send body params in x-www-form-urlencoded



          Header Content-Type:application/x-www-form-urlencoded



          According to this, you cannot send files or multipart data in the PUT method



          EDIT




          The content type "application/x-www-form-urlencoded" is inefficient
          for sending large quantities of binary data or text containing
          non-ASCII characters. The content type "multipart/form-data" should be
          used for submitting forms that contain files, non-ASCII data, and
          binary data.




          Which means if you have to submit




          files, non-ASCII data, and binary data




          you should use POST method






          share|improve this answer





















          • 2





            Why was this not upvoted? If true, this is a critical distinction is it not?

            – Matt1776
            Sep 29 '18 at 3:02






          • 2





            I faced it when implementing API for the profile update, which includes user profile pic upload. Then I tested it with the postman, Ajax, PHP curl and laravel 5.6 as backend.

            – Rohit Dhiman
            Sep 29 '18 at 6:54



















          6














          If you are familiar with database operations,
          there are




          1. Select

          2. Insert

          3. Update

          4. Delete

          5. Merge (Update if already existing, else insert)


          I use PUT for Merge and update like operations and use POST for Insertions.






          share|improve this answer































            6














            Here's a simple rule:



            PUT to a URL should be used to update or create the resource that can be located at that URL.



            POST to a URL should be used to update or create a resource which is located at some other ("subordinate") URL, or is not locatable via HTTP.






            share|improve this answer





















            • 1





              PUT is not for update, it is for replace, note that to create you are replacing nothing with something. POST is absolutely not for update in any shape of form.

              – thecoshman
              Jun 8 '15 at 8:10






            • 1





              Does the http spec say that? Or are you basing your comment on something else?

              – Adam Griffiths
              Jul 10 '16 at 20:41











            • It's just common sense, how you update something when you don't know what it is you are updating? POST is for creating a new resource.

              – thecoshman
              Jul 27 '16 at 9:23






            • 2





              thecoshman -- you are abusing semantics here -- a replace can be an update if it is the same resource with a few differences. A replace is only valid for put if replace is used to change the same resource. Replacing with a new and different resource is invalid (remove old and add new?), especially if the 'new' resource doesn't have a natural ID. POST, OTOH, is something that can create, update, replace, and delete -- using post depends on whether or not there is a message to interpret, such as 'apply the discount', which may or may not change the resource depending on logic.

              – Gerard ONeill
              Dec 28 '16 at 16:54











            • As for your second comment -- how about you 'get' the resource, modify the fields you need to, and then put it back? Or how about if the resource comes from a different source but uses a natural ID (the external ID) -- put would naturally update the resource at the URL when the original data changed.

              – Gerard ONeill
              Dec 28 '16 at 16:56



















            5














            In practice, POST works well for creating resources. The URL of the newly created resource should be returned in the Location response header. PUT should be used for updating a resource completely. Please understand that these are the best practices when designing a RESTful API. HTTP specification as such does not restrict using PUT/POST with a few restrictions for creating/updating resources. Take a look at http://techoctave.com/c7/posts/71-twitter-rest-api-dissected that summarizes the best practices.






            share|improve this answer


























            • For the most part, from reading through all this noise, you seem on the ball. I would say though, we should refer to PUT as the replace method, rather than the create/update. I think it better describes in one what it does.

              – thecoshman
              Jun 8 '15 at 8:15















            1 2
            next


            protected by Bala R May 25 '11 at 3:44



            Thank you for your interest in this question.
            Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



            Would you like to answer one of these unanswered questions instead?














            32 Answers
            32






            active

            oldest

            votes








            32 Answers
            32






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            1 2
            next










            3756














            Overall:



            Both PUT and POST can be used for creating.



            You have to ask "what are you performing the action to?" to distinguish what you should be using. Let's assume you're designing an API 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.



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



            You do not need to support both PUT and POST.



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



            Some considerations:




            • Do you name your 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 idempotent, so if you PUT an object twice, it has no effect. This is a nice property, so I would use PUT when possible.

            • 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.


            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.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
            resources 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/






            share|improve this answer





















            • 915





              I think one cannot stress enough the fact that PUT is idempotent: if the network is botched and the client is not sure whether his request made it through, it can just send it a second (or 100th) time, and it is guaranteed by the HTTP spec that this has exactly the same effect as sending once.

              – Jörg W Mittag
              Mar 10 '09 at 15:17






            • 71





              @Jörg W Mittag: Not necessary. The second time could return 409 Conflict or something if the request has been modified in meantime (by some other user or the first request itself, which got through).

              – Mitar
              Nov 27 '11 at 23:28






            • 564





              If I'm not mistaken, what we should be stressing is that PUT is defined to be idempotent. You still have to write your server in such a way that PUT behaves correctly, yes? Perhaps it's better to say "PUT causes the transport to assume idempotence, which may affect behavior of the transport, e.g. caching."

              – Ian Ni-Lewis
              Dec 28 '11 at 2:05








            • 133





              @JörgWMittag Idempotence catchphrase? How about "Send and send and send my friend, it makes no difference in the end."

              – James Beninger
              Mar 3 '14 at 17:13








            • 20





              Thinks of them as: PUT = insert or update; POST = insert. So when you make two PUT - you get the one new record, when you do two POSTs - you get two new records.

              – Eugen Konkov
              Aug 22 '16 at 9:34
















            3756














            Overall:



            Both PUT and POST can be used for creating.



            You have to ask "what are you performing the action to?" to distinguish what you should be using. Let's assume you're designing an API 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.



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



            You do not need to support both PUT and POST.



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



            Some considerations:




            • Do you name your 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 idempotent, so if you PUT an object twice, it has no effect. This is a nice property, so I would use PUT when possible.

            • 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.


            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.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
            resources 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/






            share|improve this answer





















            • 915





              I think one cannot stress enough the fact that PUT is idempotent: if the network is botched and the client is not sure whether his request made it through, it can just send it a second (or 100th) time, and it is guaranteed by the HTTP spec that this has exactly the same effect as sending once.

              – Jörg W Mittag
              Mar 10 '09 at 15:17






            • 71





              @Jörg W Mittag: Not necessary. The second time could return 409 Conflict or something if the request has been modified in meantime (by some other user or the first request itself, which got through).

              – Mitar
              Nov 27 '11 at 23:28






            • 564





              If I'm not mistaken, what we should be stressing is that PUT is defined to be idempotent. You still have to write your server in such a way that PUT behaves correctly, yes? Perhaps it's better to say "PUT causes the transport to assume idempotence, which may affect behavior of the transport, e.g. caching."

              – Ian Ni-Lewis
              Dec 28 '11 at 2:05








            • 133





              @JörgWMittag Idempotence catchphrase? How about "Send and send and send my friend, it makes no difference in the end."

              – James Beninger
              Mar 3 '14 at 17:13








            • 20





              Thinks of them as: PUT = insert or update; POST = insert. So when you make two PUT - you get the one new record, when you do two POSTs - you get two new records.

              – Eugen Konkov
              Aug 22 '16 at 9:34














            3756












            3756








            3756







            Overall:



            Both PUT and POST can be used for creating.



            You have to ask "what are you performing the action to?" to distinguish what you should be using. Let's assume you're designing an API 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.



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



            You do not need to support both PUT and POST.



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



            Some considerations:




            • Do you name your 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 idempotent, so if you PUT an object twice, it has no effect. This is a nice property, so I would use PUT when possible.

            • 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.


            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.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
            resources 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/






            share|improve this answer















            Overall:



            Both PUT and POST can be used for creating.



            You have to ask "what are you performing the action to?" to distinguish what you should be using. Let's assume you're designing an API 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.



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



            You do not need to support both PUT and POST.



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



            Some considerations:




            • Do you name your 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 idempotent, so if you PUT an object twice, it has no effect. This is a nice property, so I would use PUT when possible.

            • 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.


            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.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
            resources 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/







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 23 '17 at 11:55









            Community

            11




            11










            answered Mar 10 '09 at 14:29









            Brian R. BondyBrian R. Bondy

            253k98543594




            253k98543594








            • 915





              I think one cannot stress enough the fact that PUT is idempotent: if the network is botched and the client is not sure whether his request made it through, it can just send it a second (or 100th) time, and it is guaranteed by the HTTP spec that this has exactly the same effect as sending once.

              – Jörg W Mittag
              Mar 10 '09 at 15:17






            • 71





              @Jörg W Mittag: Not necessary. The second time could return 409 Conflict or something if the request has been modified in meantime (by some other user or the first request itself, which got through).

              – Mitar
              Nov 27 '11 at 23:28






            • 564





              If I'm not mistaken, what we should be stressing is that PUT is defined to be idempotent. You still have to write your server in such a way that PUT behaves correctly, yes? Perhaps it's better to say "PUT causes the transport to assume idempotence, which may affect behavior of the transport, e.g. caching."

              – Ian Ni-Lewis
              Dec 28 '11 at 2:05








            • 133





              @JörgWMittag Idempotence catchphrase? How about "Send and send and send my friend, it makes no difference in the end."

              – James Beninger
              Mar 3 '14 at 17:13








            • 20





              Thinks of them as: PUT = insert or update; POST = insert. So when you make two PUT - you get the one new record, when you do two POSTs - you get two new records.

              – Eugen Konkov
              Aug 22 '16 at 9:34














            • 915





              I think one cannot stress enough the fact that PUT is idempotent: if the network is botched and the client is not sure whether his request made it through, it can just send it a second (or 100th) time, and it is guaranteed by the HTTP spec that this has exactly the same effect as sending once.

              – Jörg W Mittag
              Mar 10 '09 at 15:17






            • 71





              @Jörg W Mittag: Not necessary. The second time could return 409 Conflict or something if the request has been modified in meantime (by some other user or the first request itself, which got through).

              – Mitar
              Nov 27 '11 at 23:28






            • 564





              If I'm not mistaken, what we should be stressing is that PUT is defined to be idempotent. You still have to write your server in such a way that PUT behaves correctly, yes? Perhaps it's better to say "PUT causes the transport to assume idempotence, which may affect behavior of the transport, e.g. caching."

              – Ian Ni-Lewis
              Dec 28 '11 at 2:05








            • 133





              @JörgWMittag Idempotence catchphrase? How about "Send and send and send my friend, it makes no difference in the end."

              – James Beninger
              Mar 3 '14 at 17:13








            • 20





              Thinks of them as: PUT = insert or update; POST = insert. So when you make two PUT - you get the one new record, when you do two POSTs - you get two new records.

              – Eugen Konkov
              Aug 22 '16 at 9:34








            915




            915





            I think one cannot stress enough the fact that PUT is idempotent: if the network is botched and the client is not sure whether his request made it through, it can just send it a second (or 100th) time, and it is guaranteed by the HTTP spec that this has exactly the same effect as sending once.

            – Jörg W Mittag
            Mar 10 '09 at 15:17





            I think one cannot stress enough the fact that PUT is idempotent: if the network is botched and the client is not sure whether his request made it through, it can just send it a second (or 100th) time, and it is guaranteed by the HTTP spec that this has exactly the same effect as sending once.

            – Jörg W Mittag
            Mar 10 '09 at 15:17




            71




            71





            @Jörg W Mittag: Not necessary. The second time could return 409 Conflict or something if the request has been modified in meantime (by some other user or the first request itself, which got through).

            – Mitar
            Nov 27 '11 at 23:28





            @Jörg W Mittag: Not necessary. The second time could return 409 Conflict or something if the request has been modified in meantime (by some other user or the first request itself, which got through).

            – Mitar
            Nov 27 '11 at 23:28




            564




            564





            If I'm not mistaken, what we should be stressing is that PUT is defined to be idempotent. You still have to write your server in such a way that PUT behaves correctly, yes? Perhaps it's better to say "PUT causes the transport to assume idempotence, which may affect behavior of the transport, e.g. caching."

            – Ian Ni-Lewis
            Dec 28 '11 at 2:05







            If I'm not mistaken, what we should be stressing is that PUT is defined to be idempotent. You still have to write your server in such a way that PUT behaves correctly, yes? Perhaps it's better to say "PUT causes the transport to assume idempotence, which may affect behavior of the transport, e.g. caching."

            – Ian Ni-Lewis
            Dec 28 '11 at 2:05






            133




            133





            @JörgWMittag Idempotence catchphrase? How about "Send and send and send my friend, it makes no difference in the end."

            – James Beninger
            Mar 3 '14 at 17:13







            @JörgWMittag Idempotence catchphrase? How about "Send and send and send my friend, it makes no difference in the end."

            – James Beninger
            Mar 3 '14 at 17:13






            20




            20





            Thinks of them as: PUT = insert or update; POST = insert. So when you make two PUT - you get the one new record, when you do two POSTs - you get two new records.

            – Eugen Konkov
            Aug 22 '16 at 9:34





            Thinks of them as: PUT = insert or update; POST = insert. So when you make two PUT - you get the one new record, when you do two POSTs - you get two new records.

            – Eugen Konkov
            Aug 22 '16 at 9:34













            2002














            You can find assertions on the web that say





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


            • PUT should be used to create a resource, and POST should be 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 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 this argument, 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.



            so:



            POST /expense-report


            or:



            PUT  /expense-report/10929





            share|improve this answer





















            • 63





              I agree, wherever idempotence is concerned it should trump any other concerns since getting that wrong can cause many many unexpected bugs.

              – Josh
              Oct 26 '10 at 5:56






            • 13





              If POST can update a resource, how is that not idempotent? If I change a students age using PUT and do that 10x times the students age is the same if I did it once.

              – Schneider
              May 6 '11 at 10:54






            • 23





              @Schneider, in this case your server is making an extra effort to guarantee idempotence, but it is not advertising it. Browsers will still warn the user if they try to reload such a POST request.

              – Tobu
              Jan 6 '12 at 10:53






            • 43





              @Schneider POST may create a subsidiary resource; hence you can POST to collection, like POST /expense-reports and it would create as many entities (expense reports) on your server as the quantity of requests you've sent, even if they are completely similar. Think of it as inserting the same row in the DB table (/expense-reports) with auto-incremented primary key. Data remains the same, key (URI in this case) is generated by server and is different for every other insert (request). So, POST effect can be idempotent, but also may not. Hence, POST is not idempotent.

              – Snifff
              Jan 26 '12 at 17:32








            • 11





              Let's say we have entities which may have two properties - name and date. If we have an entity with an existing name and date, but then make requests to it specifying only a name, the proper behavior of PUT would be to obliterate the date of the entity, whereas POST may update only the properties specified, leaving the unspecified properties as they were before the request was made. Does that sound correct/reasonable, or is it an improper use of PUT (I saw references to PATCH, which it seems would be more appropriate, but doesn't exist yet)?

              – Jon z
              May 8 '13 at 18:28


















            2002














            You can find assertions on the web that say





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


            • PUT should be used to create a resource, and POST should be 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 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 this argument, 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.



            so:



            POST /expense-report


            or:



            PUT  /expense-report/10929





            share|improve this answer





















            • 63





              I agree, wherever idempotence is concerned it should trump any other concerns since getting that wrong can cause many many unexpected bugs.

              – Josh
              Oct 26 '10 at 5:56






            • 13





              If POST can update a resource, how is that not idempotent? If I change a students age using PUT and do that 10x times the students age is the same if I did it once.

              – Schneider
              May 6 '11 at 10:54






            • 23





              @Schneider, in this case your server is making an extra effort to guarantee idempotence, but it is not advertising it. Browsers will still warn the user if they try to reload such a POST request.

              – Tobu
              Jan 6 '12 at 10:53






            • 43





              @Schneider POST may create a subsidiary resource; hence you can POST to collection, like POST /expense-reports and it would create as many entities (expense reports) on your server as the quantity of requests you've sent, even if they are completely similar. Think of it as inserting the same row in the DB table (/expense-reports) with auto-incremented primary key. Data remains the same, key (URI in this case) is generated by server and is different for every other insert (request). So, POST effect can be idempotent, but also may not. Hence, POST is not idempotent.

              – Snifff
              Jan 26 '12 at 17:32








            • 11





              Let's say we have entities which may have two properties - name and date. If we have an entity with an existing name and date, but then make requests to it specifying only a name, the proper behavior of PUT would be to obliterate the date of the entity, whereas POST may update only the properties specified, leaving the unspecified properties as they were before the request was made. Does that sound correct/reasonable, or is it an improper use of PUT (I saw references to PATCH, which it seems would be more appropriate, but doesn't exist yet)?

              – Jon z
              May 8 '13 at 18:28
















            2002












            2002








            2002







            You can find assertions on the web that say





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


            • PUT should be used to create a resource, and POST should be 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 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 this argument, 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.



            so:



            POST /expense-report


            or:



            PUT  /expense-report/10929





            share|improve this answer















            You can find assertions on the web that say





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


            • PUT should be used to create a resource, and POST should be 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 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 this argument, 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.



            so:



            POST /expense-report


            or:



            PUT  /expense-report/10929






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 22 '13 at 5:56









            the Tin Man

            135k27173256




            135k27173256










            answered Apr 22 '10 at 14:55









            CheesoCheeso

            135k74404635




            135k74404635








            • 63





              I agree, wherever idempotence is concerned it should trump any other concerns since getting that wrong can cause many many unexpected bugs.

              – Josh
              Oct 26 '10 at 5:56






            • 13





              If POST can update a resource, how is that not idempotent? If I change a students age using PUT and do that 10x times the students age is the same if I did it once.

              – Schneider
              May 6 '11 at 10:54






            • 23





              @Schneider, in this case your server is making an extra effort to guarantee idempotence, but it is not advertising it. Browsers will still warn the user if they try to reload such a POST request.

              – Tobu
              Jan 6 '12 at 10:53






            • 43





              @Schneider POST may create a subsidiary resource; hence you can POST to collection, like POST /expense-reports and it would create as many entities (expense reports) on your server as the quantity of requests you've sent, even if they are completely similar. Think of it as inserting the same row in the DB table (/expense-reports) with auto-incremented primary key. Data remains the same, key (URI in this case) is generated by server and is different for every other insert (request). So, POST effect can be idempotent, but also may not. Hence, POST is not idempotent.

              – Snifff
              Jan 26 '12 at 17:32








            • 11





              Let's say we have entities which may have two properties - name and date. If we have an entity with an existing name and date, but then make requests to it specifying only a name, the proper behavior of PUT would be to obliterate the date of the entity, whereas POST may update only the properties specified, leaving the unspecified properties as they were before the request was made. Does that sound correct/reasonable, or is it an improper use of PUT (I saw references to PATCH, which it seems would be more appropriate, but doesn't exist yet)?

              – Jon z
              May 8 '13 at 18:28
















            • 63





              I agree, wherever idempotence is concerned it should trump any other concerns since getting that wrong can cause many many unexpected bugs.

              – Josh
              Oct 26 '10 at 5:56






            • 13





              If POST can update a resource, how is that not idempotent? If I change a students age using PUT and do that 10x times the students age is the same if I did it once.

              – Schneider
              May 6 '11 at 10:54






            • 23





              @Schneider, in this case your server is making an extra effort to guarantee idempotence, but it is not advertising it. Browsers will still warn the user if they try to reload such a POST request.

              – Tobu
              Jan 6 '12 at 10:53






            • 43





              @Schneider POST may create a subsidiary resource; hence you can POST to collection, like POST /expense-reports and it would create as many entities (expense reports) on your server as the quantity of requests you've sent, even if they are completely similar. Think of it as inserting the same row in the DB table (/expense-reports) with auto-incremented primary key. Data remains the same, key (URI in this case) is generated by server and is different for every other insert (request). So, POST effect can be idempotent, but also may not. Hence, POST is not idempotent.

              – Snifff
              Jan 26 '12 at 17:32








            • 11





              Let's say we have entities which may have two properties - name and date. If we have an entity with an existing name and date, but then make requests to it specifying only a name, the proper behavior of PUT would be to obliterate the date of the entity, whereas POST may update only the properties specified, leaving the unspecified properties as they were before the request was made. Does that sound correct/reasonable, or is it an improper use of PUT (I saw references to PATCH, which it seems would be more appropriate, but doesn't exist yet)?

              – Jon z
              May 8 '13 at 18:28










            63




            63





            I agree, wherever idempotence is concerned it should trump any other concerns since getting that wrong can cause many many unexpected bugs.

            – Josh
            Oct 26 '10 at 5:56





            I agree, wherever idempotence is concerned it should trump any other concerns since getting that wrong can cause many many unexpected bugs.

            – Josh
            Oct 26 '10 at 5:56




            13




            13





            If POST can update a resource, how is that not idempotent? If I change a students age using PUT and do that 10x times the students age is the same if I did it once.

            – Schneider
            May 6 '11 at 10:54





            If POST can update a resource, how is that not idempotent? If I change a students age using PUT and do that 10x times the students age is the same if I did it once.

            – Schneider
            May 6 '11 at 10:54




            23




            23





            @Schneider, in this case your server is making an extra effort to guarantee idempotence, but it is not advertising it. Browsers will still warn the user if they try to reload such a POST request.

            – Tobu
            Jan 6 '12 at 10:53





            @Schneider, in this case your server is making an extra effort to guarantee idempotence, but it is not advertising it. Browsers will still warn the user if they try to reload such a POST request.

            – Tobu
            Jan 6 '12 at 10:53




            43




            43





            @Schneider POST may create a subsidiary resource; hence you can POST to collection, like POST /expense-reports and it would create as many entities (expense reports) on your server as the quantity of requests you've sent, even if they are completely similar. Think of it as inserting the same row in the DB table (/expense-reports) with auto-incremented primary key. Data remains the same, key (URI in this case) is generated by server and is different for every other insert (request). So, POST effect can be idempotent, but also may not. Hence, POST is not idempotent.

            – Snifff
            Jan 26 '12 at 17:32







            @Schneider POST may create a subsidiary resource; hence you can POST to collection, like POST /expense-reports and it would create as many entities (expense reports) on your server as the quantity of requests you've sent, even if they are completely similar. Think of it as inserting the same row in the DB table (/expense-reports) with auto-incremented primary key. Data remains the same, key (URI in this case) is generated by server and is different for every other insert (request). So, POST effect can be idempotent, but also may not. Hence, POST is not idempotent.

            – Snifff
            Jan 26 '12 at 17:32






            11




            11





            Let's say we have entities which may have two properties - name and date. If we have an entity with an existing name and date, but then make requests to it specifying only a name, the proper behavior of PUT would be to obliterate the date of the entity, whereas POST may update only the properties specified, leaving the unspecified properties as they were before the request was made. Does that sound correct/reasonable, or is it an improper use of PUT (I saw references to PATCH, which it seems would be more appropriate, but doesn't exist yet)?

            – Jon z
            May 8 '13 at 18:28







            Let's say we have entities which may have two properties - name and date. If we have an entity with an existing name and date, but then make requests to it specifying only a name, the proper behavior of PUT would be to obliterate the date of the entity, whereas POST may update only the properties specified, leaving the unspecified properties as they were before the request was made. Does that sound correct/reasonable, or is it an improper use of PUT (I saw references to PATCH, which it seems would be more appropriate, but doesn't exist yet)?

            – Jon z
            May 8 '13 at 18:28













            619
















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


            • PUT to a URL creates/replaces the resource in its entirety at the client defined URL.


            • PATCH to a URL updates part of the resource at that client defined URL.


            The relevant specification for PUT and POST is 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 will create two resources.



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



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



            PUT replaces the resource at the known url if it already exists, so sending the same request twice has no effect. In other words, calls to PUT are 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 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. If the server desires that the request be applied to a different URI,




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



            Update 2018: There is a case that can be made to avoid PUT. See "REST without PUT"




            With “REST without PUT” technique, the idea is that consumers are
            forced to post new 'nounified' request resources. As discussed
            earlier, changing a customer’s mailing address is a POST to a new
            “ChangeOfAddress” resource, not a PUT of a “Customer” resource with a
            different mailing address field value.




            taken from REST API Design - Resource Modeling by Prakash Subramaniam of Thoughtworks



            This forces the API to avoid state transition problems with multiple clients updating a single resource, and matches more nicely with event sourcing and CQRS. When the work is done asynchronously, POSTing the transformation and waiting for it to be applied seems appropriate.






            share|improve this answer





















            • 46





              Or from the other side of the fence: PUT if the client determines the resulting resource's address, POST if the server does it.

              – DanMan
              Nov 28 '12 at 19:47






            • 3





              I think that this answer should be edited to make it more clear what @DanMan pointed in a very simple way. What I find the most valuable here is the note at the end, stating that a PUT should be used only for replacing the whole resource.

              – Hermes
              Nov 26 '13 at 22:37






            • 2





              PATCH isn't a realistic option for at least a few years, but I agree with the ideology.

              – crush
              Oct 3 '14 at 17:33






            • 4





              I'm trying to understand, but using PUT to create something would only make sense if the client knows for sure that the resource doesn't exist yet, right? Following the blog example, say you have created hundreds of blog posts in a couple of years, then accidentally pick the same title as you did for a post two years ago. Now you have gone and replaced that post, which wasn't intended. So using PUT to create would require the client to track what is taken and what is not, and could lead to accidents and unintended side effects, as well as having routes that do two entirely different things?

              – galaxyAbstractor
              Jan 30 '15 at 9:01








            • 4





              You are correct. PUTting a blog post at the same url as an existing one would cause an update to that existing post (although you could obviously check first with a GET). This indicates why it would be a bad idea to use just the title as the URL. It would however work anywhere there was a natural key in the data... which in my experience is rare. Or if you used GUIDs

              – Nigel Thorne
              Feb 3 '15 at 5:20


















            619
















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


            • PUT to a URL creates/replaces the resource in its entirety at the client defined URL.


            • PATCH to a URL updates part of the resource at that client defined URL.


            The relevant specification for PUT and POST is 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 will create two resources.



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



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



            PUT replaces the resource at the known url if it already exists, so sending the same request twice has no effect. In other words, calls to PUT are 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 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. If the server desires that the request be applied to a different URI,




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



            Update 2018: There is a case that can be made to avoid PUT. See "REST without PUT"




            With “REST without PUT” technique, the idea is that consumers are
            forced to post new 'nounified' request resources. As discussed
            earlier, changing a customer’s mailing address is a POST to a new
            “ChangeOfAddress” resource, not a PUT of a “Customer” resource with a
            different mailing address field value.




            taken from REST API Design - Resource Modeling by Prakash Subramaniam of Thoughtworks



            This forces the API to avoid state transition problems with multiple clients updating a single resource, and matches more nicely with event sourcing and CQRS. When the work is done asynchronously, POSTing the transformation and waiting for it to be applied seems appropriate.






            share|improve this answer





















            • 46





              Or from the other side of the fence: PUT if the client determines the resulting resource's address, POST if the server does it.

              – DanMan
              Nov 28 '12 at 19:47






            • 3





              I think that this answer should be edited to make it more clear what @DanMan pointed in a very simple way. What I find the most valuable here is the note at the end, stating that a PUT should be used only for replacing the whole resource.

              – Hermes
              Nov 26 '13 at 22:37






            • 2





              PATCH isn't a realistic option for at least a few years, but I agree with the ideology.

              – crush
              Oct 3 '14 at 17:33






            • 4





              I'm trying to understand, but using PUT to create something would only make sense if the client knows for sure that the resource doesn't exist yet, right? Following the blog example, say you have created hundreds of blog posts in a couple of years, then accidentally pick the same title as you did for a post two years ago. Now you have gone and replaced that post, which wasn't intended. So using PUT to create would require the client to track what is taken and what is not, and could lead to accidents and unintended side effects, as well as having routes that do two entirely different things?

              – galaxyAbstractor
              Jan 30 '15 at 9:01








            • 4





              You are correct. PUTting a blog post at the same url as an existing one would cause an update to that existing post (although you could obviously check first with a GET). This indicates why it would be a bad idea to use just the title as the URL. It would however work anywhere there was a natural key in the data... which in my experience is rare. Or if you used GUIDs

              – Nigel Thorne
              Feb 3 '15 at 5:20
















            619












            619








            619









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


            • PUT to a URL creates/replaces the resource in its entirety at the client defined URL.


            • PATCH to a URL updates part of the resource at that client defined URL.


            The relevant specification for PUT and POST is 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 will create two resources.



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



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



            PUT replaces the resource at the known url if it already exists, so sending the same request twice has no effect. In other words, calls to PUT are 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 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. If the server desires that the request be applied to a different URI,




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



            Update 2018: There is a case that can be made to avoid PUT. See "REST without PUT"




            With “REST without PUT” technique, the idea is that consumers are
            forced to post new 'nounified' request resources. As discussed
            earlier, changing a customer’s mailing address is a POST to a new
            “ChangeOfAddress” resource, not a PUT of a “Customer” resource with a
            different mailing address field value.




            taken from REST API Design - Resource Modeling by Prakash Subramaniam of Thoughtworks



            This forces the API to avoid state transition problems with multiple clients updating a single resource, and matches more nicely with event sourcing and CQRS. When the work is done asynchronously, POSTing the transformation and waiting for it to be applied seems appropriate.






            share|improve this answer

















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


            • PUT to a URL creates/replaces the resource in its entirety at the client defined URL.


            • PATCH to a URL updates part of the resource at that client defined URL.


            The relevant specification for PUT and POST is 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 will create two resources.



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



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



            PUT replaces the resource at the known url if it already exists, so sending the same request twice has no effect. In other words, calls to PUT are 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 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. If the server desires that the request be applied to a different URI,




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



            Update 2018: There is a case that can be made to avoid PUT. See "REST without PUT"




            With “REST without PUT” technique, the idea is that consumers are
            forced to post new 'nounified' request resources. As discussed
            earlier, changing a customer’s mailing address is a POST to a new
            “ChangeOfAddress” resource, not a PUT of a “Customer” resource with a
            different mailing address field value.




            taken from REST API Design - Resource Modeling by Prakash Subramaniam of Thoughtworks



            This forces the API to avoid state transition problems with multiple clients updating a single resource, and matches more nicely with event sourcing and CQRS. When the work is done asynchronously, POSTing the transformation and waiting for it to be applied seems appropriate.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 26 '18 at 1:52

























            answered Apr 7 '10 at 5:52









            Nigel ThorneNigel Thorne

            16.4k32547




            16.4k32547








            • 46





              Or from the other side of the fence: PUT if the client determines the resulting resource's address, POST if the server does it.

              – DanMan
              Nov 28 '12 at 19:47






            • 3





              I think that this answer should be edited to make it more clear what @DanMan pointed in a very simple way. What I find the most valuable here is the note at the end, stating that a PUT should be used only for replacing the whole resource.

              – Hermes
              Nov 26 '13 at 22:37






            • 2





              PATCH isn't a realistic option for at least a few years, but I agree with the ideology.

              – crush
              Oct 3 '14 at 17:33






            • 4





              I'm trying to understand, but using PUT to create something would only make sense if the client knows for sure that the resource doesn't exist yet, right? Following the blog example, say you have created hundreds of blog posts in a couple of years, then accidentally pick the same title as you did for a post two years ago. Now you have gone and replaced that post, which wasn't intended. So using PUT to create would require the client to track what is taken and what is not, and could lead to accidents and unintended side effects, as well as having routes that do two entirely different things?

              – galaxyAbstractor
              Jan 30 '15 at 9:01








            • 4





              You are correct. PUTting a blog post at the same url as an existing one would cause an update to that existing post (although you could obviously check first with a GET). This indicates why it would be a bad idea to use just the title as the URL. It would however work anywhere there was a natural key in the data... which in my experience is rare. Or if you used GUIDs

              – Nigel Thorne
              Feb 3 '15 at 5:20
















            • 46





              Or from the other side of the fence: PUT if the client determines the resulting resource's address, POST if the server does it.

              – DanMan
              Nov 28 '12 at 19:47






            • 3





              I think that this answer should be edited to make it more clear what @DanMan pointed in a very simple way. What I find the most valuable here is the note at the end, stating that a PUT should be used only for replacing the whole resource.

              – Hermes
              Nov 26 '13 at 22:37






            • 2





              PATCH isn't a realistic option for at least a few years, but I agree with the ideology.

              – crush
              Oct 3 '14 at 17:33






            • 4





              I'm trying to understand, but using PUT to create something would only make sense if the client knows for sure that the resource doesn't exist yet, right? Following the blog example, say you have created hundreds of blog posts in a couple of years, then accidentally pick the same title as you did for a post two years ago. Now you have gone and replaced that post, which wasn't intended. So using PUT to create would require the client to track what is taken and what is not, and could lead to accidents and unintended side effects, as well as having routes that do two entirely different things?

              – galaxyAbstractor
              Jan 30 '15 at 9:01








            • 4





              You are correct. PUTting a blog post at the same url as an existing one would cause an update to that existing post (although you could obviously check first with a GET). This indicates why it would be a bad idea to use just the title as the URL. It would however work anywhere there was a natural key in the data... which in my experience is rare. Or if you used GUIDs

              – Nigel Thorne
              Feb 3 '15 at 5:20










            46




            46





            Or from the other side of the fence: PUT if the client determines the resulting resource's address, POST if the server does it.

            – DanMan
            Nov 28 '12 at 19:47





            Or from the other side of the fence: PUT if the client determines the resulting resource's address, POST if the server does it.

            – DanMan
            Nov 28 '12 at 19:47




            3




            3





            I think that this answer should be edited to make it more clear what @DanMan pointed in a very simple way. What I find the most valuable here is the note at the end, stating that a PUT should be used only for replacing the whole resource.

            – Hermes
            Nov 26 '13 at 22:37





            I think that this answer should be edited to make it more clear what @DanMan pointed in a very simple way. What I find the most valuable here is the note at the end, stating that a PUT should be used only for replacing the whole resource.

            – Hermes
            Nov 26 '13 at 22:37




            2




            2





            PATCH isn't a realistic option for at least a few years, but I agree with the ideology.

            – crush
            Oct 3 '14 at 17:33





            PATCH isn't a realistic option for at least a few years, but I agree with the ideology.

            – crush
            Oct 3 '14 at 17:33




            4




            4





            I'm trying to understand, but using PUT to create something would only make sense if the client knows for sure that the resource doesn't exist yet, right? Following the blog example, say you have created hundreds of blog posts in a couple of years, then accidentally pick the same title as you did for a post two years ago. Now you have gone and replaced that post, which wasn't intended. So using PUT to create would require the client to track what is taken and what is not, and could lead to accidents and unintended side effects, as well as having routes that do two entirely different things?

            – galaxyAbstractor
            Jan 30 '15 at 9:01







            I'm trying to understand, but using PUT to create something would only make sense if the client knows for sure that the resource doesn't exist yet, right? Following the blog example, say you have created hundreds of blog posts in a couple of years, then accidentally pick the same title as you did for a post two years ago. Now you have gone and replaced that post, which wasn't intended. So using PUT to create would require the client to track what is taken and what is not, and could lead to accidents and unintended side effects, as well as having routes that do two entirely different things?

            – galaxyAbstractor
            Jan 30 '15 at 9:01






            4




            4





            You are correct. PUTting a blog post at the same url as an existing one would cause an update to that existing post (although you could obviously check first with a GET). This indicates why it would be a bad idea to use just the title as the URL. It would however work anywhere there was a natural key in the data... which in my experience is rare. Or if you used GUIDs

            – Nigel Thorne
            Feb 3 '15 at 5:20







            You are correct. PUTting a blog post at the same url as an existing one would cause an update to that existing post (although you could obviously check first with a GET). This indicates why it would be a bad idea to use just the title as the URL. It would however work anywhere there was a natural key in the data... which in my experience is rare. Or if you used GUIDs

            – Nigel Thorne
            Feb 3 '15 at 5:20













            179














            Summary:



            Create:



            Can be performed with both PUT or POST in the following way:




            PUT



            Creates THE new resource with newResourceId as the identifier, under the /resources URI, or collection.



            PUT /resources/<newResourceId> HTTP/1.1 


            POST



            Creates A new resource under the /resources URI, or collection. Usually the identifier is returned by the server.



            POST /resources HTTP/1.1



            Update:



            Can only be performed with PUT in the following way:




            PUT



            Updates the resource with existingResourceId as the identifier, under the /resources URI, or collection.



            PUT /resources/<existingResourceId> HTTP/1.1



            Explanation:



            When dealing with REST and URI as general, you have generic on the left and specific on the right. The generics are usually called collections and the more specific items can be called resource. Note that a resource can contain a collection.




            Examples:



            <-- generic -- specific -->



            URI: website.com/users/john
            website.com - whole site
            users - collection of users
            john - item of the collection, or a resource

            URI:website.com/users/john/posts/23
            website.com - whole site
            users - collection of users
            john - item of the collection, or a resource
            posts - collection of posts from john
            23 - post from john with identifier 23, also a resource



            When you use POST you are always refering to a collection, so whenever you say:



            POST /users HTTP/1.1


            you are posting a new user to the users collection.



            If you go on and try something like this:



            POST /users/john HTTP/1.1


            it will work, but semantically you are saying that you want to add a resource to the john collection under the users collection.



            Once you are using PUT you are refering to a resource or single item, possibly inside a collection. So when you say:



            PUT /users/john HTTP/1.1


            you are telling to the server update, or create if it doesn't exist, the john resource under the users collection.



            Spec:



            Let me highlight some important parts of the spec:



            POST




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




            Hence, creates a new resource on a collection.



            PUT




            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."




            Hence, create or update based on existence of the resource.



            Reference:




            • HTTP/1.1 Spec

            • Wikipedia - REST

            • Uniform Resource Identifiers (URI): Generic Syntax and Semantics






            share|improve this answer





















            • 7





              This post was helpful to me in understanding that POST adds "something" as a child to the given collection (URI), whereas PUT explicitly defines the "something" at the given URI location.

              – kwah
              Nov 23 '13 at 16:33






            • 2





              This is the best answer, here, I think: none of this "POST can update a resource" nonsense. I like your statement, "Update can only be performed with PUT".

              – Thomas
              May 28 '15 at 13:44






            • 2





              No, PUT is not for update or create. It is for replacing. Note that you can replace nothing with something for the effect of creating.

              – thecoshman
              Jun 8 '15 at 8:07






            • 2





              @7hi4g0 PUT is for for updating with a complete replacement, in other words, it replaces. You replace nothing with something, or something with a completely new something. PUT is not for making a minor change (unless you have the client make that minor change and provide the entire new version, even what is remaining the same). For partial modification, PATCH is the method of choice.

              – thecoshman
              Jun 8 '15 at 12:57






            • 1





              @thecoshman You could, but it wouldn't be too clear that create is also covered in there. In this case, it is better to be explicit.

              – 7hi4g0
              Jun 9 '15 at 20:21
















            179














            Summary:



            Create:



            Can be performed with both PUT or POST in the following way:




            PUT



            Creates THE new resource with newResourceId as the identifier, under the /resources URI, or collection.



            PUT /resources/<newResourceId> HTTP/1.1 


            POST



            Creates A new resource under the /resources URI, or collection. Usually the identifier is returned by the server.



            POST /resources HTTP/1.1



            Update:



            Can only be performed with PUT in the following way:




            PUT



            Updates the resource with existingResourceId as the identifier, under the /resources URI, or collection.



            PUT /resources/<existingResourceId> HTTP/1.1



            Explanation:



            When dealing with REST and URI as general, you have generic on the left and specific on the right. The generics are usually called collections and the more specific items can be called resource. Note that a resource can contain a collection.




            Examples:



            <-- generic -- specific -->



            URI: website.com/users/john
            website.com - whole site
            users - collection of users
            john - item of the collection, or a resource

            URI:website.com/users/john/posts/23
            website.com - whole site
            users - collection of users
            john - item of the collection, or a resource
            posts - collection of posts from john
            23 - post from john with identifier 23, also a resource



            When you use POST you are always refering to a collection, so whenever you say:



            POST /users HTTP/1.1


            you are posting a new user to the users collection.



            If you go on and try something like this:



            POST /users/john HTTP/1.1


            it will work, but semantically you are saying that you want to add a resource to the john collection under the users collection.



            Once you are using PUT you are refering to a resource or single item, possibly inside a collection. So when you say:



            PUT /users/john HTTP/1.1


            you are telling to the server update, or create if it doesn't exist, the john resource under the users collection.



            Spec:



            Let me highlight some important parts of the spec:



            POST




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




            Hence, creates a new resource on a collection.



            PUT




            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."




            Hence, create or update based on existence of the resource.



            Reference:




            • HTTP/1.1 Spec

            • Wikipedia - REST

            • Uniform Resource Identifiers (URI): Generic Syntax and Semantics






            share|improve this answer





















            • 7





              This post was helpful to me in understanding that POST adds "something" as a child to the given collection (URI), whereas PUT explicitly defines the "something" at the given URI location.

              – kwah
              Nov 23 '13 at 16:33






            • 2





              This is the best answer, here, I think: none of this "POST can update a resource" nonsense. I like your statement, "Update can only be performed with PUT".

              – Thomas
              May 28 '15 at 13:44






            • 2





              No, PUT is not for update or create. It is for replacing. Note that you can replace nothing with something for the effect of creating.

              – thecoshman
              Jun 8 '15 at 8:07






            • 2





              @7hi4g0 PUT is for for updating with a complete replacement, in other words, it replaces. You replace nothing with something, or something with a completely new something. PUT is not for making a minor change (unless you have the client make that minor change and provide the entire new version, even what is remaining the same). For partial modification, PATCH is the method of choice.

              – thecoshman
              Jun 8 '15 at 12:57






            • 1





              @thecoshman You could, but it wouldn't be too clear that create is also covered in there. In this case, it is better to be explicit.

              – 7hi4g0
              Jun 9 '15 at 20:21














            179












            179








            179







            Summary:



            Create:



            Can be performed with both PUT or POST in the following way:




            PUT



            Creates THE new resource with newResourceId as the identifier, under the /resources URI, or collection.



            PUT /resources/<newResourceId> HTTP/1.1 


            POST



            Creates A new resource under the /resources URI, or collection. Usually the identifier is returned by the server.



            POST /resources HTTP/1.1



            Update:



            Can only be performed with PUT in the following way:




            PUT



            Updates the resource with existingResourceId as the identifier, under the /resources URI, or collection.



            PUT /resources/<existingResourceId> HTTP/1.1



            Explanation:



            When dealing with REST and URI as general, you have generic on the left and specific on the right. The generics are usually called collections and the more specific items can be called resource. Note that a resource can contain a collection.




            Examples:



            <-- generic -- specific -->



            URI: website.com/users/john
            website.com - whole site
            users - collection of users
            john - item of the collection, or a resource

            URI:website.com/users/john/posts/23
            website.com - whole site
            users - collection of users
            john - item of the collection, or a resource
            posts - collection of posts from john
            23 - post from john with identifier 23, also a resource



            When you use POST you are always refering to a collection, so whenever you say:



            POST /users HTTP/1.1


            you are posting a new user to the users collection.



            If you go on and try something like this:



            POST /users/john HTTP/1.1


            it will work, but semantically you are saying that you want to add a resource to the john collection under the users collection.



            Once you are using PUT you are refering to a resource or single item, possibly inside a collection. So when you say:



            PUT /users/john HTTP/1.1


            you are telling to the server update, or create if it doesn't exist, the john resource under the users collection.



            Spec:



            Let me highlight some important parts of the spec:



            POST




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




            Hence, creates a new resource on a collection.



            PUT




            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."




            Hence, create or update based on existence of the resource.



            Reference:




            • HTTP/1.1 Spec

            • Wikipedia - REST

            • Uniform Resource Identifiers (URI): Generic Syntax and Semantics






            share|improve this answer















            Summary:



            Create:



            Can be performed with both PUT or POST in the following way:




            PUT



            Creates THE new resource with newResourceId as the identifier, under the /resources URI, or collection.



            PUT /resources/<newResourceId> HTTP/1.1 


            POST



            Creates A new resource under the /resources URI, or collection. Usually the identifier is returned by the server.



            POST /resources HTTP/1.1



            Update:



            Can only be performed with PUT in the following way:




            PUT



            Updates the resource with existingResourceId as the identifier, under the /resources URI, or collection.



            PUT /resources/<existingResourceId> HTTP/1.1



            Explanation:



            When dealing with REST and URI as general, you have generic on the left and specific on the right. The generics are usually called collections and the more specific items can be called resource. Note that a resource can contain a collection.




            Examples:



            <-- generic -- specific -->



            URI: website.com/users/john
            website.com - whole site
            users - collection of users
            john - item of the collection, or a resource

            URI:website.com/users/john/posts/23
            website.com - whole site
            users - collection of users
            john - item of the collection, or a resource
            posts - collection of posts from john
            23 - post from john with identifier 23, also a resource



            When you use POST you are always refering to a collection, so whenever you say:



            POST /users HTTP/1.1


            you are posting a new user to the users collection.



            If you go on and try something like this:



            POST /users/john HTTP/1.1


            it will work, but semantically you are saying that you want to add a resource to the john collection under the users collection.



            Once you are using PUT you are refering to a resource or single item, possibly inside a collection. So when you say:



            PUT /users/john HTTP/1.1


            you are telling to the server update, or create if it doesn't exist, the john resource under the users collection.



            Spec:



            Let me highlight some important parts of the spec:



            POST




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




            Hence, creates a new resource on a collection.



            PUT




            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."




            Hence, create or update based on existence of the resource.



            Reference:




            • HTTP/1.1 Spec

            • Wikipedia - REST

            • Uniform Resource Identifiers (URI): Generic Syntax and Semantics







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 9 '16 at 18:25









            varDumper

            1,1391915




            1,1391915










            answered Aug 14 '13 at 22:47









            7hi4g07hi4g0

            2,33421529




            2,33421529








            • 7





              This post was helpful to me in understanding that POST adds "something" as a child to the given collection (URI), whereas PUT explicitly defines the "something" at the given URI location.

              – kwah
              Nov 23 '13 at 16:33






            • 2





              This is the best answer, here, I think: none of this "POST can update a resource" nonsense. I like your statement, "Update can only be performed with PUT".

              – Thomas
              May 28 '15 at 13:44






            • 2





              No, PUT is not for update or create. It is for replacing. Note that you can replace nothing with something for the effect of creating.

              – thecoshman
              Jun 8 '15 at 8:07






            • 2





              @7hi4g0 PUT is for for updating with a complete replacement, in other words, it replaces. You replace nothing with something, or something with a completely new something. PUT is not for making a minor change (unless you have the client make that minor change and provide the entire new version, even what is remaining the same). For partial modification, PATCH is the method of choice.

              – thecoshman
              Jun 8 '15 at 12:57






            • 1





              @thecoshman You could, but it wouldn't be too clear that create is also covered in there. In this case, it is better to be explicit.

              – 7hi4g0
              Jun 9 '15 at 20:21














            • 7





              This post was helpful to me in understanding that POST adds "something" as a child to the given collection (URI), whereas PUT explicitly defines the "something" at the given URI location.

              – kwah
              Nov 23 '13 at 16:33






            • 2





              This is the best answer, here, I think: none of this "POST can update a resource" nonsense. I like your statement, "Update can only be performed with PUT".

              – Thomas
              May 28 '15 at 13:44






            • 2





              No, PUT is not for update or create. It is for replacing. Note that you can replace nothing with something for the effect of creating.

              – thecoshman
              Jun 8 '15 at 8:07






            • 2





              @7hi4g0 PUT is for for updating with a complete replacement, in other words, it replaces. You replace nothing with something, or something with a completely new something. PUT is not for making a minor change (unless you have the client make that minor change and provide the entire new version, even what is remaining the same). For partial modification, PATCH is the method of choice.

              – thecoshman
              Jun 8 '15 at 12:57






            • 1





              @thecoshman You could, but it wouldn't be too clear that create is also covered in there. In this case, it is better to be explicit.

              – 7hi4g0
              Jun 9 '15 at 20:21








            7




            7





            This post was helpful to me in understanding that POST adds "something" as a child to the given collection (URI), whereas PUT explicitly defines the "something" at the given URI location.

            – kwah
            Nov 23 '13 at 16:33





            This post was helpful to me in understanding that POST adds "something" as a child to the given collection (URI), whereas PUT explicitly defines the "something" at the given URI location.

            – kwah
            Nov 23 '13 at 16:33




            2




            2





            This is the best answer, here, I think: none of this "POST can update a resource" nonsense. I like your statement, "Update can only be performed with PUT".

            – Thomas
            May 28 '15 at 13:44





            This is the best answer, here, I think: none of this "POST can update a resource" nonsense. I like your statement, "Update can only be performed with PUT".

            – Thomas
            May 28 '15 at 13:44




            2




            2





            No, PUT is not for update or create. It is for replacing. Note that you can replace nothing with something for the effect of creating.

            – thecoshman
            Jun 8 '15 at 8:07





            No, PUT is not for update or create. It is for replacing. Note that you can replace nothing with something for the effect of creating.

            – thecoshman
            Jun 8 '15 at 8:07




            2




            2





            @7hi4g0 PUT is for for updating with a complete replacement, in other words, it replaces. You replace nothing with something, or something with a completely new something. PUT is not for making a minor change (unless you have the client make that minor change and provide the entire new version, even what is remaining the same). For partial modification, PATCH is the method of choice.

            – thecoshman
            Jun 8 '15 at 12:57





            @7hi4g0 PUT is for for updating with a complete replacement, in other words, it replaces. You replace nothing with something, or something with a completely new something. PUT is not for making a minor change (unless you have the client make that minor change and provide the entire new version, even what is remaining the same). For partial modification, PATCH is the method of choice.

            – thecoshman
            Jun 8 '15 at 12:57




            1




            1





            @thecoshman You could, but it wouldn't be too clear that create is also covered in there. In this case, it is better to be explicit.

            – 7hi4g0
            Jun 9 '15 at 20:21





            @thecoshman You could, but it wouldn't be too clear that create is also covered in there. In this case, it is better to be explicit.

            – 7hi4g0
            Jun 9 '15 at 20:21











            163














            I'd like to add my "pragmatic" advice. Use PUT when you know the "id" by which the object you are saving can be retrieved. Using PUT won't work too well if you need, say, a database generated id to be returned for you to do future lookups or updates.



            So: To save an existing user, or one where the client generates the id and it's been verified that the id is unique:



            PUT /user/12345 HTTP/1.1  <-- create the user providing the id 12345
            Host: mydomain.com

            GET /user/12345 HTTP/1.1 <-- return that user
            Host: mydomain.com


            Otherwise, use POST to initially create the object, and PUT to update the object:



            POST /user HTTP/1.1   <--- create the user, server returns 12345
            Host: mydomain.com

            PUT /user/12345 HTTP/1.1 <--- update the user
            Host: mydomain.com





            share|improve this answer





















            • 14





              Actually, it should be POST /users. (Note that /users is plural.) This has the affect of creating a new user and making it a child resource of the /users collection.

              – DavidRR
              Dec 16 '14 at 13:54








            • 5





              @DavidRR to be fair, how to handle groups is another debate altogether. GET /users makes sense, it reads as you want, but I'd be ok with GET /user/<id> or POST /user (with payload for said new user) because it reads correctly 'get me users 5' is odd, but 'get me user 5' is more natural. I'd probably still fall down on the side of pluralisation though :)

              – thecoshman
              Jun 8 '15 at 7:57
















            163














            I'd like to add my "pragmatic" advice. Use PUT when you know the "id" by which the object you are saving can be retrieved. Using PUT won't work too well if you need, say, a database generated id to be returned for you to do future lookups or updates.



            So: To save an existing user, or one where the client generates the id and it's been verified that the id is unique:



            PUT /user/12345 HTTP/1.1  <-- create the user providing the id 12345
            Host: mydomain.com

            GET /user/12345 HTTP/1.1 <-- return that user
            Host: mydomain.com


            Otherwise, use POST to initially create the object, and PUT to update the object:



            POST /user HTTP/1.1   <--- create the user, server returns 12345
            Host: mydomain.com

            PUT /user/12345 HTTP/1.1 <--- update the user
            Host: mydomain.com





            share|improve this answer





















            • 14





              Actually, it should be POST /users. (Note that /users is plural.) This has the affect of creating a new user and making it a child resource of the /users collection.

              – DavidRR
              Dec 16 '14 at 13:54








            • 5





              @DavidRR to be fair, how to handle groups is another debate altogether. GET /users makes sense, it reads as you want, but I'd be ok with GET /user/<id> or POST /user (with payload for said new user) because it reads correctly 'get me users 5' is odd, but 'get me user 5' is more natural. I'd probably still fall down on the side of pluralisation though :)

              – thecoshman
              Jun 8 '15 at 7:57














            163












            163








            163







            I'd like to add my "pragmatic" advice. Use PUT when you know the "id" by which the object you are saving can be retrieved. Using PUT won't work too well if you need, say, a database generated id to be returned for you to do future lookups or updates.



            So: To save an existing user, or one where the client generates the id and it's been verified that the id is unique:



            PUT /user/12345 HTTP/1.1  <-- create the user providing the id 12345
            Host: mydomain.com

            GET /user/12345 HTTP/1.1 <-- return that user
            Host: mydomain.com


            Otherwise, use POST to initially create the object, and PUT to update the object:



            POST /user HTTP/1.1   <--- create the user, server returns 12345
            Host: mydomain.com

            PUT /user/12345 HTTP/1.1 <--- update the user
            Host: mydomain.com





            share|improve this answer















            I'd like to add my "pragmatic" advice. Use PUT when you know the "id" by which the object you are saving can be retrieved. Using PUT won't work too well if you need, say, a database generated id to be returned for you to do future lookups or updates.



            So: To save an existing user, or one where the client generates the id and it's been verified that the id is unique:



            PUT /user/12345 HTTP/1.1  <-- create the user providing the id 12345
            Host: mydomain.com

            GET /user/12345 HTTP/1.1 <-- return that user
            Host: mydomain.com


            Otherwise, use POST to initially create the object, and PUT to update the object:



            POST /user HTTP/1.1   <--- create the user, server returns 12345
            Host: mydomain.com

            PUT /user/12345 HTTP/1.1 <--- update the user
            Host: mydomain.com






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 25 '11 at 3:43

























            answered Jan 15 '11 at 19:59









            ThaDonThaDon

            3,80673965




            3,80673965








            • 14





              Actually, it should be POST /users. (Note that /users is plural.) This has the affect of creating a new user and making it a child resource of the /users collection.

              – DavidRR
              Dec 16 '14 at 13:54








            • 5





              @DavidRR to be fair, how to handle groups is another debate altogether. GET /users makes sense, it reads as you want, but I'd be ok with GET /user/<id> or POST /user (with payload for said new user) because it reads correctly 'get me users 5' is odd, but 'get me user 5' is more natural. I'd probably still fall down on the side of pluralisation though :)

              – thecoshman
              Jun 8 '15 at 7:57














            • 14





              Actually, it should be POST /users. (Note that /users is plural.) This has the affect of creating a new user and making it a child resource of the /users collection.

              – DavidRR
              Dec 16 '14 at 13:54








            • 5





              @DavidRR to be fair, how to handle groups is another debate altogether. GET /users makes sense, it reads as you want, but I'd be ok with GET /user/<id> or POST /user (with payload for said new user) because it reads correctly 'get me users 5' is odd, but 'get me user 5' is more natural. I'd probably still fall down on the side of pluralisation though :)

              – thecoshman
              Jun 8 '15 at 7:57








            14




            14





            Actually, it should be POST /users. (Note that /users is plural.) This has the affect of creating a new user and making it a child resource of the /users collection.

            – DavidRR
            Dec 16 '14 at 13:54







            Actually, it should be POST /users. (Note that /users is plural.) This has the affect of creating a new user and making it a child resource of the /users collection.

            – DavidRR
            Dec 16 '14 at 13:54






            5




            5





            @DavidRR to be fair, how to handle groups is another debate altogether. GET /users makes sense, it reads as you want, but I'd be ok with GET /user/<id> or POST /user (with payload for said new user) because it reads correctly 'get me users 5' is odd, but 'get me user 5' is more natural. I'd probably still fall down on the side of pluralisation though :)

            – thecoshman
            Jun 8 '15 at 7:57





            @DavidRR to be fair, how to handle groups is another debate altogether. GET /users makes sense, it reads as you want, but I'd be ok with GET /user/<id> or POST /user (with payload for said new user) because it reads correctly 'get me users 5' is odd, but 'get me user 5' is more natural. I'd probably still fall down on the side of pluralisation though :)

            – thecoshman
            Jun 8 '15 at 7:57











            155














            POST means "create new" as in "Here is the input for creating a user, create it for me".



            PUT means "insert, replace if already exists" as in "Here is the data for user 5".



            You POST to example.com/users since you don't know the URL of the user yet, you want the server to create it.



            You PUT to example.com/users/id since you want to replace/create a specific user.



            POSTing twice with the same data means create two identical users with different ids. PUTing twice with the same data creates the user the first and updates him to the same state the second time (no changes). Since you end up with the same state after a PUT no matter how many times you perform it, it is said to be "equally potent" every time - idempotent. This is useful for automatically retrying requests. No more 'are you sure you want to resend' when you push the back button on the browser.



            A general advice is to use POST when you need the server to be in control of URL generation of your resources. Use PUT otherwise. Prefer PUT over POST.






            share|improve this answer





















            • 8





              Sloppiness may have cause it to be commonly taught that there are only two verbs you need: GET and POST. GET to obtain, POST to change. Even PUT and DELETE were performed using POST. Asking what PUT really means 25 years later maybe a sign we learned it wrong at first. REST popularity drove people back to the basics where we must now unlearn past bad mistakes. POST was overused and now commonly taught incorrectly. Best part: "POSTing twice with the same data means create two identical [resources]". Great point!

              – maxpolk
              Sep 1 '14 at 19:36








            • 1





              How can you use PUT to create a record by the ID, like in your example user 5 if it doesn't exist yet? Don't you mean update, replace if already exists? or something

              – Luke
              Nov 28 '14 at 12:44













            • @Coulton: I meant what I wrote. You insert user 5 if you PUT to /users/5 and #5 does not exist yet.

              – Alexander Torstling
              Nov 28 '14 at 15:46











            • @Coulton: And PUT can also be used to replace the value of an existing resource in its entirety.

              – DavidRR
              Dec 16 '14 at 14:02








            • 1





              "Prefer PUT over POST"... care to justify that?

              – thecoshman
              Jun 8 '15 at 8:04
















            155














            POST means "create new" as in "Here is the input for creating a user, create it for me".



            PUT means "insert, replace if already exists" as in "Here is the data for user 5".



            You POST to example.com/users since you don't know the URL of the user yet, you want the server to create it.



            You PUT to example.com/users/id since you want to replace/create a specific user.



            POSTing twice with the same data means create two identical users with different ids. PUTing twice with the same data creates the user the first and updates him to the same state the second time (no changes). Since you end up with the same state after a PUT no matter how many times you perform it, it is said to be "equally potent" every time - idempotent. This is useful for automatically retrying requests. No more 'are you sure you want to resend' when you push the back button on the browser.



            A general advice is to use POST when you need the server to be in control of URL generation of your resources. Use PUT otherwise. Prefer PUT over POST.






            share|improve this answer





















            • 8





              Sloppiness may have cause it to be commonly taught that there are only two verbs you need: GET and POST. GET to obtain, POST to change. Even PUT and DELETE were performed using POST. Asking what PUT really means 25 years later maybe a sign we learned it wrong at first. REST popularity drove people back to the basics where we must now unlearn past bad mistakes. POST was overused and now commonly taught incorrectly. Best part: "POSTing twice with the same data means create two identical [resources]". Great point!

              – maxpolk
              Sep 1 '14 at 19:36








            • 1





              How can you use PUT to create a record by the ID, like in your example user 5 if it doesn't exist yet? Don't you mean update, replace if already exists? or something

              – Luke
              Nov 28 '14 at 12:44













            • @Coulton: I meant what I wrote. You insert user 5 if you PUT to /users/5 and #5 does not exist yet.

              – Alexander Torstling
              Nov 28 '14 at 15:46











            • @Coulton: And PUT can also be used to replace the value of an existing resource in its entirety.

              – DavidRR
              Dec 16 '14 at 14:02








            • 1





              "Prefer PUT over POST"... care to justify that?

              – thecoshman
              Jun 8 '15 at 8:04














            155












            155








            155







            POST means "create new" as in "Here is the input for creating a user, create it for me".



            PUT means "insert, replace if already exists" as in "Here is the data for user 5".



            You POST to example.com/users since you don't know the URL of the user yet, you want the server to create it.



            You PUT to example.com/users/id since you want to replace/create a specific user.



            POSTing twice with the same data means create two identical users with different ids. PUTing twice with the same data creates the user the first and updates him to the same state the second time (no changes). Since you end up with the same state after a PUT no matter how many times you perform it, it is said to be "equally potent" every time - idempotent. This is useful for automatically retrying requests. No more 'are you sure you want to resend' when you push the back button on the browser.



            A general advice is to use POST when you need the server to be in control of URL generation of your resources. Use PUT otherwise. Prefer PUT over POST.






            share|improve this answer















            POST means "create new" as in "Here is the input for creating a user, create it for me".



            PUT means "insert, replace if already exists" as in "Here is the data for user 5".



            You POST to example.com/users since you don't know the URL of the user yet, you want the server to create it.



            You PUT to example.com/users/id since you want to replace/create a specific user.



            POSTing twice with the same data means create two identical users with different ids. PUTing twice with the same data creates the user the first and updates him to the same state the second time (no changes). Since you end up with the same state after a PUT no matter how many times you perform it, it is said to be "equally potent" every time - idempotent. This is useful for automatically retrying requests. No more 'are you sure you want to resend' when you push the back button on the browser.



            A general advice is to use POST when you need the server to be in control of URL generation of your resources. Use PUT otherwise. Prefer PUT over POST.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Apr 23 '17 at 18:13

























            answered Oct 23 '11 at 14:27









            Alexander TorstlingAlexander Torstling

            13.8k64865




            13.8k64865








            • 8





              Sloppiness may have cause it to be commonly taught that there are only two verbs you need: GET and POST. GET to obtain, POST to change. Even PUT and DELETE were performed using POST. Asking what PUT really means 25 years later maybe a sign we learned it wrong at first. REST popularity drove people back to the basics where we must now unlearn past bad mistakes. POST was overused and now commonly taught incorrectly. Best part: "POSTing twice with the same data means create two identical [resources]". Great point!

              – maxpolk
              Sep 1 '14 at 19:36








            • 1





              How can you use PUT to create a record by the ID, like in your example user 5 if it doesn't exist yet? Don't you mean update, replace if already exists? or something

              – Luke
              Nov 28 '14 at 12:44













            • @Coulton: I meant what I wrote. You insert user 5 if you PUT to /users/5 and #5 does not exist yet.

              – Alexander Torstling
              Nov 28 '14 at 15:46











            • @Coulton: And PUT can also be used to replace the value of an existing resource in its entirety.

              – DavidRR
              Dec 16 '14 at 14:02








            • 1





              "Prefer PUT over POST"... care to justify that?

              – thecoshman
              Jun 8 '15 at 8:04














            • 8





              Sloppiness may have cause it to be commonly taught that there are only two verbs you need: GET and POST. GET to obtain, POST to change. Even PUT and DELETE were performed using POST. Asking what PUT really means 25 years later maybe a sign we learned it wrong at first. REST popularity drove people back to the basics where we must now unlearn past bad mistakes. POST was overused and now commonly taught incorrectly. Best part: "POSTing twice with the same data means create two identical [resources]". Great point!

              – maxpolk
              Sep 1 '14 at 19:36








            • 1





              How can you use PUT to create a record by the ID, like in your example user 5 if it doesn't exist yet? Don't you mean update, replace if already exists? or something

              – Luke
              Nov 28 '14 at 12:44













            • @Coulton: I meant what I wrote. You insert user 5 if you PUT to /users/5 and #5 does not exist yet.

              – Alexander Torstling
              Nov 28 '14 at 15:46











            • @Coulton: And PUT can also be used to replace the value of an existing resource in its entirety.

              – DavidRR
              Dec 16 '14 at 14:02








            • 1





              "Prefer PUT over POST"... care to justify that?

              – thecoshman
              Jun 8 '15 at 8:04








            8




            8





            Sloppiness may have cause it to be commonly taught that there are only two verbs you need: GET and POST. GET to obtain, POST to change. Even PUT and DELETE were performed using POST. Asking what PUT really means 25 years later maybe a sign we learned it wrong at first. REST popularity drove people back to the basics where we must now unlearn past bad mistakes. POST was overused and now commonly taught incorrectly. Best part: "POSTing twice with the same data means create two identical [resources]". Great point!

            – maxpolk
            Sep 1 '14 at 19:36







            Sloppiness may have cause it to be commonly taught that there are only two verbs you need: GET and POST. GET to obtain, POST to change. Even PUT and DELETE were performed using POST. Asking what PUT really means 25 years later maybe a sign we learned it wrong at first. REST popularity drove people back to the basics where we must now unlearn past bad mistakes. POST was overused and now commonly taught incorrectly. Best part: "POSTing twice with the same data means create two identical [resources]". Great point!

            – maxpolk
            Sep 1 '14 at 19:36






            1




            1





            How can you use PUT to create a record by the ID, like in your example user 5 if it doesn't exist yet? Don't you mean update, replace if already exists? or something

            – Luke
            Nov 28 '14 at 12:44







            How can you use PUT to create a record by the ID, like in your example user 5 if it doesn't exist yet? Don't you mean update, replace if already exists? or something

            – Luke
            Nov 28 '14 at 12:44















            @Coulton: I meant what I wrote. You insert user 5 if you PUT to /users/5 and #5 does not exist yet.

            – Alexander Torstling
            Nov 28 '14 at 15:46





            @Coulton: I meant what I wrote. You insert user 5 if you PUT to /users/5 and #5 does not exist yet.

            – Alexander Torstling
            Nov 28 '14 at 15:46













            @Coulton: And PUT can also be used to replace the value of an existing resource in its entirety.

            – DavidRR
            Dec 16 '14 at 14:02







            @Coulton: And PUT can also be used to replace the value of an existing resource in its entirety.

            – DavidRR
            Dec 16 '14 at 14:02






            1




            1





            "Prefer PUT over POST"... care to justify that?

            – thecoshman
            Jun 8 '15 at 8:04





            "Prefer PUT over POST"... care to justify that?

            – thecoshman
            Jun 8 '15 at 8:04











            117














            Use POST to create, and PUT to update. That's how Ruby on Rails is doing it, anyway.



            PUT    /items/1      #=> update
            POST /items #=> create





            share|improve this answer





















            • 4





              POST /items adds a new item to an already defined resource ('item'). It does not, as the answer says, "create a group." I don't understand why this has 12 votes.

              – David J.
              Jun 21 '12 at 5:26













            • Out of the box, Rails does not support 'creating a group' via REST. To 'create a group' by which I mean 'create a resource' you have to do it via the source code.

              – David J.
              Jun 21 '12 at 5:28






            • 8





              This is a fair guideline, but an oversimplification. As the other answers mention, either method could be used for both create and update.

              – Brad Koch
              Mar 7 '13 at 15:55






            • 2





              I agree with the answer with a slight modification. Use POST to create and PUT to update the resource completely. For partial updates, we can use PUT or PATCH. Lets say we want to update the status of a group. We can use PUT /groups/1/status with the status is the request payload or PATCH /groups/1 with the details about the action in the payload

              – java_geek
              Oct 6 '14 at 6:26






            • 2





              It should also be made clear that PUT /items/42 is also valid for creating a resource, but only if the client has the privilege of naming the resource. (Does Rails allow a client this naming privilege?)

              – DavidRR
              Dec 16 '14 at 14:10


















            117














            Use POST to create, and PUT to update. That's how Ruby on Rails is doing it, anyway.



            PUT    /items/1      #=> update
            POST /items #=> create





            share|improve this answer





















            • 4





              POST /items adds a new item to an already defined resource ('item'). It does not, as the answer says, "create a group." I don't understand why this has 12 votes.

              – David J.
              Jun 21 '12 at 5:26













            • Out of the box, Rails does not support 'creating a group' via REST. To 'create a group' by which I mean 'create a resource' you have to do it via the source code.

              – David J.
              Jun 21 '12 at 5:28






            • 8





              This is a fair guideline, but an oversimplification. As the other answers mention, either method could be used for both create and update.

              – Brad Koch
              Mar 7 '13 at 15:55






            • 2





              I agree with the answer with a slight modification. Use POST to create and PUT to update the resource completely. For partial updates, we can use PUT or PATCH. Lets say we want to update the status of a group. We can use PUT /groups/1/status with the status is the request payload or PATCH /groups/1 with the details about the action in the payload

              – java_geek
              Oct 6 '14 at 6:26






            • 2





              It should also be made clear that PUT /items/42 is also valid for creating a resource, but only if the client has the privilege of naming the resource. (Does Rails allow a client this naming privilege?)

              – DavidRR
              Dec 16 '14 at 14:10
















            117












            117








            117







            Use POST to create, and PUT to update. That's how Ruby on Rails is doing it, anyway.



            PUT    /items/1      #=> update
            POST /items #=> create





            share|improve this answer















            Use POST to create, and PUT to update. That's how Ruby on Rails is doing it, anyway.



            PUT    /items/1      #=> update
            POST /items #=> create






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Sep 16 '17 at 20:17









            Peter Mortensen

            13.7k1986111




            13.7k1986111










            answered Mar 10 '09 at 14:28









            Tim SullivanTim Sullivan

            13.3k1170111




            13.3k1170111








            • 4





              POST /items adds a new item to an already defined resource ('item'). It does not, as the answer says, "create a group." I don't understand why this has 12 votes.

              – David J.
              Jun 21 '12 at 5:26













            • Out of the box, Rails does not support 'creating a group' via REST. To 'create a group' by which I mean 'create a resource' you have to do it via the source code.

              – David J.
              Jun 21 '12 at 5:28






            • 8





              This is a fair guideline, but an oversimplification. As the other answers mention, either method could be used for both create and update.

              – Brad Koch
              Mar 7 '13 at 15:55






            • 2





              I agree with the answer with a slight modification. Use POST to create and PUT to update the resource completely. For partial updates, we can use PUT or PATCH. Lets say we want to update the status of a group. We can use PUT /groups/1/status with the status is the request payload or PATCH /groups/1 with the details about the action in the payload

              – java_geek
              Oct 6 '14 at 6:26






            • 2





              It should also be made clear that PUT /items/42 is also valid for creating a resource, but only if the client has the privilege of naming the resource. (Does Rails allow a client this naming privilege?)

              – DavidRR
              Dec 16 '14 at 14:10
















            • 4





              POST /items adds a new item to an already defined resource ('item'). It does not, as the answer says, "create a group." I don't understand why this has 12 votes.

              – David J.
              Jun 21 '12 at 5:26













            • Out of the box, Rails does not support 'creating a group' via REST. To 'create a group' by which I mean 'create a resource' you have to do it via the source code.

              – David J.
              Jun 21 '12 at 5:28






            • 8





              This is a fair guideline, but an oversimplification. As the other answers mention, either method could be used for both create and update.

              – Brad Koch
              Mar 7 '13 at 15:55






            • 2





              I agree with the answer with a slight modification. Use POST to create and PUT to update the resource completely. For partial updates, we can use PUT or PATCH. Lets say we want to update the status of a group. We can use PUT /groups/1/status with the status is the request payload or PATCH /groups/1 with the details about the action in the payload

              – java_geek
              Oct 6 '14 at 6:26






            • 2





              It should also be made clear that PUT /items/42 is also valid for creating a resource, but only if the client has the privilege of naming the resource. (Does Rails allow a client this naming privilege?)

              – DavidRR
              Dec 16 '14 at 14:10










            4




            4





            POST /items adds a new item to an already defined resource ('item'). It does not, as the answer says, "create a group." I don't understand why this has 12 votes.

            – David J.
            Jun 21 '12 at 5:26







            POST /items adds a new item to an already defined resource ('item'). It does not, as the answer says, "create a group." I don't understand why this has 12 votes.

            – David J.
            Jun 21 '12 at 5:26















            Out of the box, Rails does not support 'creating a group' via REST. To 'create a group' by which I mean 'create a resource' you have to do it via the source code.

            – David J.
            Jun 21 '12 at 5:28





            Out of the box, Rails does not support 'creating a group' via REST. To 'create a group' by which I mean 'create a resource' you have to do it via the source code.

            – David J.
            Jun 21 '12 at 5:28




            8




            8





            This is a fair guideline, but an oversimplification. As the other answers mention, either method could be used for both create and update.

            – Brad Koch
            Mar 7 '13 at 15:55





            This is a fair guideline, but an oversimplification. As the other answers mention, either method could be used for both create and update.

            – Brad Koch
            Mar 7 '13 at 15:55




            2




            2





            I agree with the answer with a slight modification. Use POST to create and PUT to update the resource completely. For partial updates, we can use PUT or PATCH. Lets say we want to update the status of a group. We can use PUT /groups/1/status with the status is the request payload or PATCH /groups/1 with the details about the action in the payload

            – java_geek
            Oct 6 '14 at 6:26





            I agree with the answer with a slight modification. Use POST to create and PUT to update the resource completely. For partial updates, we can use PUT or PATCH. Lets say we want to update the status of a group. We can use PUT /groups/1/status with the status is the request payload or PATCH /groups/1 with the details about the action in the payload

            – java_geek
            Oct 6 '14 at 6:26




            2




            2





            It should also be made clear that PUT /items/42 is also valid for creating a resource, but only if the client has the privilege of naming the resource. (Does Rails allow a client this naming privilege?)

            – DavidRR
            Dec 16 '14 at 14:10







            It should also be made clear that PUT /items/42 is also valid for creating a resource, but only if the client has the privilege of naming the resource. (Does Rails allow a client this naming privilege?)

            – DavidRR
            Dec 16 '14 at 14:10













            70














            Both are used for data transmission between client to server, but there are subtle differences between them, which are:



            Enter image description here



            Analogy:




            • PUT i.e. take and put where it was.

            • POST as send mail in post office.


            enter image description here






            share|improve this answer





















            • 5





              So in summary: POST for inserts, PUT for updates

              – MobileMon
              Sep 22 '15 at 12:31






            • 14





              @MobileMon No, REST methods are not CRUD.

              – jlr
              Jan 8 '16 at 19:06











            • I'd say PUT for UPSERTS

              – Hola Soy Edu Feliz Navidad
              Nov 26 '18 at 11:45
















            70














            Both are used for data transmission between client to server, but there are subtle differences between them, which are:



            Enter image description here



            Analogy:




            • PUT i.e. take and put where it was.

            • POST as send mail in post office.


            enter image description here






            share|improve this answer





















            • 5





              So in summary: POST for inserts, PUT for updates

              – MobileMon
              Sep 22 '15 at 12:31






            • 14





              @MobileMon No, REST methods are not CRUD.

              – jlr
              Jan 8 '16 at 19:06











            • I'd say PUT for UPSERTS

              – Hola Soy Edu Feliz Navidad
              Nov 26 '18 at 11:45














            70












            70








            70







            Both are used for data transmission between client to server, but there are subtle differences between them, which are:



            Enter image description here



            Analogy:




            • PUT i.e. take and put where it was.

            • POST as send mail in post office.


            enter image description here






            share|improve this answer















            Both are used for data transmission between client to server, but there are subtle differences between them, which are:



            Enter image description here



            Analogy:




            • PUT i.e. take and put where it was.

            • POST as send mail in post office.


            enter image description here







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 7 '18 at 8:46

























            answered Sep 11 '15 at 13:12









            PremrajPremraj

            30.5k12158117




            30.5k12158117








            • 5





              So in summary: POST for inserts, PUT for updates

              – MobileMon
              Sep 22 '15 at 12:31






            • 14





              @MobileMon No, REST methods are not CRUD.

              – jlr
              Jan 8 '16 at 19:06











            • I'd say PUT for UPSERTS

              – Hola Soy Edu Feliz Navidad
              Nov 26 '18 at 11:45














            • 5





              So in summary: POST for inserts, PUT for updates

              – MobileMon
              Sep 22 '15 at 12:31






            • 14





              @MobileMon No, REST methods are not CRUD.

              – jlr
              Jan 8 '16 at 19:06











            • I'd say PUT for UPSERTS

              – Hola Soy Edu Feliz Navidad
              Nov 26 '18 at 11:45








            5




            5





            So in summary: POST for inserts, PUT for updates

            – MobileMon
            Sep 22 '15 at 12:31





            So in summary: POST for inserts, PUT for updates

            – MobileMon
            Sep 22 '15 at 12:31




            14




            14





            @MobileMon No, REST methods are not CRUD.

            – jlr
            Jan 8 '16 at 19:06





            @MobileMon No, REST methods are not CRUD.

            – jlr
            Jan 8 '16 at 19:06













            I'd say PUT for UPSERTS

            – Hola Soy Edu Feliz Navidad
            Nov 26 '18 at 11:45





            I'd say PUT for UPSERTS

            – Hola Soy Edu Feliz Navidad
            Nov 26 '18 at 11:45











            63














            REST is a very high-level concept. In fact, it doesn't even mention HTTP at all!



            If you have any doubts about how to implement REST in HTTP, you can always take a look at the Atom Publication Protocol (AtomPub) specification. AtomPub is a standard for writing RESTful webservices with HTTP that was developed by many HTTP and REST luminaries, with some input from Roy Fielding, the inventor of REST and (co-)inventor of HTTP himself.



            In fact, you might even be able to use AtomPub directly. While it came out of the blogging community, it is in no way restricted to blogging: it is a generic protocol for RESTfully interacting with arbitrary (nested) collections of arbitrary resources via HTTP. If you can represent your application as a nested collection of resources, then you can just use AtomPub and not worry about whether to use PUT or POST, what HTTP Status Codes to return and all those details.



            This is what AtomPub has to say about resource creation (section 9.2):




            To add members to a Collection, clients send POST requests to the URI of the Collection.







            share|improve this answer





















            • 7





              There's nothing wrong with allowing PUT to create resources. Just be aware that it means that the client provides the URL.

              – Julian Reschke
              Apr 7 '10 at 7:47






            • 5





              There's something very wrong with allowing PUT to create resources: the client provides the URL. That's the server's job!

              – Joshcodes
              Oct 29 '13 at 17:33











            • @Joshcodes It is not always the case that it is the server's job to create client ids. I have increasingly seen designs that let clients generate some sort of UUID as the resource id. This design lends itself in particular to increase scale.

              – Justin Ohms
              Feb 5 '17 at 18:26











            • @JustinOhms I agree with your point about client generated IDs (side note: all systems designed by me since circa 2008 require the client to create the ID as a UUID/Guid). That does not mean the client should specify the URL.

              – Joshcodes
              Feb 6 '17 at 18:24






            • 1





              Yes, if the resource already exists, use PUT. However, in nearly all cases, the resources should be created with POST and the client should not provide the URL. Roy Fielding agrees with this statement FWIW: roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven

              – Joshcodes
              Feb 7 '17 at 1:26
















            63














            REST is a very high-level concept. In fact, it doesn't even mention HTTP at all!



            If you have any doubts about how to implement REST in HTTP, you can always take a look at the Atom Publication Protocol (AtomPub) specification. AtomPub is a standard for writing RESTful webservices with HTTP that was developed by many HTTP and REST luminaries, with some input from Roy Fielding, the inventor of REST and (co-)inventor of HTTP himself.



            In fact, you might even be able to use AtomPub directly. While it came out of the blogging community, it is in no way restricted to blogging: it is a generic protocol for RESTfully interacting with arbitrary (nested) collections of arbitrary resources via HTTP. If you can represent your application as a nested collection of resources, then you can just use AtomPub and not worry about whether to use PUT or POST, what HTTP Status Codes to return and all those details.



            This is what AtomPub has to say about resource creation (section 9.2):




            To add members to a Collection, clients send POST requests to the URI of the Collection.







            share|improve this answer





















            • 7





              There's nothing wrong with allowing PUT to create resources. Just be aware that it means that the client provides the URL.

              – Julian Reschke
              Apr 7 '10 at 7:47






            • 5





              There's something very wrong with allowing PUT to create resources: the client provides the URL. That's the server's job!

              – Joshcodes
              Oct 29 '13 at 17:33











            • @Joshcodes It is not always the case that it is the server's job to create client ids. I have increasingly seen designs that let clients generate some sort of UUID as the resource id. This design lends itself in particular to increase scale.

              – Justin Ohms
              Feb 5 '17 at 18:26











            • @JustinOhms I agree with your point about client generated IDs (side note: all systems designed by me since circa 2008 require the client to create the ID as a UUID/Guid). That does not mean the client should specify the URL.

              – Joshcodes
              Feb 6 '17 at 18:24






            • 1





              Yes, if the resource already exists, use PUT. However, in nearly all cases, the resources should be created with POST and the client should not provide the URL. Roy Fielding agrees with this statement FWIW: roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven

              – Joshcodes
              Feb 7 '17 at 1:26














            63












            63








            63







            REST is a very high-level concept. In fact, it doesn't even mention HTTP at all!



            If you have any doubts about how to implement REST in HTTP, you can always take a look at the Atom Publication Protocol (AtomPub) specification. AtomPub is a standard for writing RESTful webservices with HTTP that was developed by many HTTP and REST luminaries, with some input from Roy Fielding, the inventor of REST and (co-)inventor of HTTP himself.



            In fact, you might even be able to use AtomPub directly. While it came out of the blogging community, it is in no way restricted to blogging: it is a generic protocol for RESTfully interacting with arbitrary (nested) collections of arbitrary resources via HTTP. If you can represent your application as a nested collection of resources, then you can just use AtomPub and not worry about whether to use PUT or POST, what HTTP Status Codes to return and all those details.



            This is what AtomPub has to say about resource creation (section 9.2):




            To add members to a Collection, clients send POST requests to the URI of the Collection.







            share|improve this answer















            REST is a very high-level concept. In fact, it doesn't even mention HTTP at all!



            If you have any doubts about how to implement REST in HTTP, you can always take a look at the Atom Publication Protocol (AtomPub) specification. AtomPub is a standard for writing RESTful webservices with HTTP that was developed by many HTTP and REST luminaries, with some input from Roy Fielding, the inventor of REST and (co-)inventor of HTTP himself.



            In fact, you might even be able to use AtomPub directly. While it came out of the blogging community, it is in no way restricted to blogging: it is a generic protocol for RESTfully interacting with arbitrary (nested) collections of arbitrary resources via HTTP. If you can represent your application as a nested collection of resources, then you can just use AtomPub and not worry about whether to use PUT or POST, what HTTP Status Codes to return and all those details.



            This is what AtomPub has to say about resource creation (section 9.2):




            To add members to a Collection, clients send POST requests to the URI of the Collection.








            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jul 27 '18 at 14:37









            Xan

            54k10106133




            54k10106133










            answered Mar 10 '09 at 15:27









            Jörg W MittagJörg W Mittag

            291k63356550




            291k63356550








            • 7





              There's nothing wrong with allowing PUT to create resources. Just be aware that it means that the client provides the URL.

              – Julian Reschke
              Apr 7 '10 at 7:47






            • 5





              There's something very wrong with allowing PUT to create resources: the client provides the URL. That's the server's job!

              – Joshcodes
              Oct 29 '13 at 17:33











            • @Joshcodes It is not always the case that it is the server's job to create client ids. I have increasingly seen designs that let clients generate some sort of UUID as the resource id. This design lends itself in particular to increase scale.

              – Justin Ohms
              Feb 5 '17 at 18:26











            • @JustinOhms I agree with your point about client generated IDs (side note: all systems designed by me since circa 2008 require the client to create the ID as a UUID/Guid). That does not mean the client should specify the URL.

              – Joshcodes
              Feb 6 '17 at 18:24






            • 1





              Yes, if the resource already exists, use PUT. However, in nearly all cases, the resources should be created with POST and the client should not provide the URL. Roy Fielding agrees with this statement FWIW: roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven

              – Joshcodes
              Feb 7 '17 at 1:26














            • 7





              There's nothing wrong with allowing PUT to create resources. Just be aware that it means that the client provides the URL.

              – Julian Reschke
              Apr 7 '10 at 7:47






            • 5





              There's something very wrong with allowing PUT to create resources: the client provides the URL. That's the server's job!

              – Joshcodes
              Oct 29 '13 at 17:33











            • @Joshcodes It is not always the case that it is the server's job to create client ids. I have increasingly seen designs that let clients generate some sort of UUID as the resource id. This design lends itself in particular to increase scale.

              – Justin Ohms
              Feb 5 '17 at 18:26











            • @JustinOhms I agree with your point about client generated IDs (side note: all systems designed by me since circa 2008 require the client to create the ID as a UUID/Guid). That does not mean the client should specify the URL.

              – Joshcodes
              Feb 6 '17 at 18:24






            • 1





              Yes, if the resource already exists, use PUT. However, in nearly all cases, the resources should be created with POST and the client should not provide the URL. Roy Fielding agrees with this statement FWIW: roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven

              – Joshcodes
              Feb 7 '17 at 1:26








            7




            7





            There's nothing wrong with allowing PUT to create resources. Just be aware that it means that the client provides the URL.

            – Julian Reschke
            Apr 7 '10 at 7:47





            There's nothing wrong with allowing PUT to create resources. Just be aware that it means that the client provides the URL.

            – Julian Reschke
            Apr 7 '10 at 7:47




            5




            5





            There's something very wrong with allowing PUT to create resources: the client provides the URL. That's the server's job!

            – Joshcodes
            Oct 29 '13 at 17:33





            There's something very wrong with allowing PUT to create resources: the client provides the URL. That's the server's job!

            – Joshcodes
            Oct 29 '13 at 17:33













            @Joshcodes It is not always the case that it is the server's job to create client ids. I have increasingly seen designs that let clients generate some sort of UUID as the resource id. This design lends itself in particular to increase scale.

            – Justin Ohms
            Feb 5 '17 at 18:26





            @Joshcodes It is not always the case that it is the server's job to create client ids. I have increasingly seen designs that let clients generate some sort of UUID as the resource id. This design lends itself in particular to increase scale.

            – Justin Ohms
            Feb 5 '17 at 18:26













            @JustinOhms I agree with your point about client generated IDs (side note: all systems designed by me since circa 2008 require the client to create the ID as a UUID/Guid). That does not mean the client should specify the URL.

            – Joshcodes
            Feb 6 '17 at 18:24





            @JustinOhms I agree with your point about client generated IDs (side note: all systems designed by me since circa 2008 require the client to create the ID as a UUID/Guid). That does not mean the client should specify the URL.

            – Joshcodes
            Feb 6 '17 at 18:24




            1




            1





            Yes, if the resource already exists, use PUT. However, in nearly all cases, the resources should be created with POST and the client should not provide the URL. Roy Fielding agrees with this statement FWIW: roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven

            – Joshcodes
            Feb 7 '17 at 1:26





            Yes, if the resource already exists, use PUT. However, in nearly all cases, the resources should be created with POST and the client should not provide the URL. Roy Fielding agrees with this statement FWIW: roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven

            – Joshcodes
            Feb 7 '17 at 1:26











            56














            The decision of whether to use PUT or POST to create a resource on a server with an HTTP + REST API is based on who owns the URL structure. Having the client know, or participate in defining, the URL struct is an unnecessary coupling akin to the undesirable couplings that arose from SOA. Escaping types of couplings is the reason REST is so popular. Therefore, the proper method to use is POST. There are exceptions to this rule and they occur when the client wishes to retain control over the location structure of the resources it deploys. This is rare and likely means something else is wrong.



            At this point some people will argue that if RESTful-URL's are used, the client does knows the URL of the resource and therefore a PUT is acceptable. After all, this is why canonical, normalized, Ruby on Rails, Django URLs are important, look at the Twitter API … blah blah blah. Those people need to understand there is no such thing as a Restful-URL and that Roy Fielding himself states that:




            A REST API must not define fixed resource names or hierarchies (an
            obvious coupling of client and server). Servers must have the freedom
            to control their own namespace. Instead, allow servers to instruct
            clients on how to construct appropriate URIs, such as is done in HTML
            forms and URI templates, by defining those instructions within media
            types and link relations. [Failure here implies that clients are
            assuming a resource structure due to out-of band information, such as
            a domain-specific standard, which is the data-oriented equivalent to
            RPC's functional coupling].



            http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven




            The idea of a RESTful-URL is actually a violation of REST as the server is in charge of the URL structure and should be free to decide how to use it to avoid coupling. If this confuses you read about the significance of self discovery on API design.



            Using POST to create resources comes with a design consideration because POST is not idempotent. This means that repeating a POST several times does not guarantee the same behavior each time. This scares people into using PUT to create resources when they should not. They know it's wrong (POST is for CREATE) but they do it anyway because they don't know how to solve this problem. This concern is demonstrated in the following situation:




            1. The client POST a new resource to the server.

            2. The server processes the request and sends a response.

            3. The client never receives the response.

            4. The server is unaware the client has not received the response.

            5. The client does not have a URL for the resource (therefore PUT is not an option) and repeats the POST.

            6. POST is not idempotent and the server …


            Step 6 is where people commonly get confused about what to do. However, there is no reason to create a kludge to solve this issue. Instead, HTTP can be used as specified in RFC 2616 and the server replies:




            10.4.10 409 Conflict



            The request could not be completed due to a conflict with the current
            state of the resource. This code is only allowed in situations where
            it is expected that the user might be able to resolve the conflict and
            resubmit the request. The response body SHOULD include enough



            information for the user to recognize the source of the conflict.
            Ideally, the response entity would include enough information for the
            user or user agent to fix the problem; however, that might not be
            possible and is not required.



            Conflicts are most likely to occur in response to a PUT request. For
            example, if versioning were being used and the entity being PUT
            included changes to a resource which conflict with those made by an
            earlier (third-party) request, the server might use the 409 response
            to indicate that it can’t complete the request. In this case, the
            response entity would likely contain a list of the differences between
            the two versions in a format defined by the response Content-Type.




            Replying with a status code of 409 Conflict is the correct recourse because:




            • Performing a POST of data which has an ID which matches a resource already in the system is “a conflict with the current state of the resource.”

            • Since the important part is for the client to understand the server has the resource and to take appropriate action. This is a “situation(s) where it is expected that the user might be able to resolve the conflict and resubmit the request.”

            • A response which contains the URL of the resource with the conflicting ID and the appropriate preconditions for the resource would provide “enough information for the user or user agent to fix the problem” which is the ideal case per RFC 2616.


            Update based on release of RFC 7231 to Replace 2616



            RFC 7231 is designed to replace 2616 and in Section 4.3.3 describes the follow possible response for a POST




            If the result of processing a POST would be equivalent to a
            representation of an existing resource, an origin server MAY redirect
            the user agent to that resource by sending a 303 (See Other) response
            with the existing resource's identifier in the Location field. This
            has the benefits of providing the user agent a resource identifier
            and transferring the representation via a method more amenable to
            shared caching, though at the cost of an extra request if the user
            agent does not already have the representation cached.




            It now may be tempting to simply return a 303 in the event that a POST is repeated. However, the opposite is true. Returning a 303 would only make sense if multiple create requests (creating different resources) return the same content. An example would be a "thank you for submitting your request message" that the client need not re-download each time. RFC 7231 still maintains in section 4.2.2 that POST is not to be idempotent and continues to maintain that POST should be used for create.



            For more information about this, read this article.






            share|improve this answer


























            • Would a 409 Conflict response be the appropriate code for something like trying to create a new account with a username that already exists? I've been using 409 for versioning conflicts specifically, but after reading your answer, I wonder if it shouldn't be used for any "duplicate" requests.

              – Eric B.
              Jul 9 '14 at 4:43











            • @EricB. Yes, in the situation you describe "due to a conflict with the current state of the resource" the operation fails. Additionally, it is reasonable to expect that the user can resolve the conflict and the message body only needs to inform the user that the username already exists.

              – Joshcodes
              Jul 10 '14 at 13:25











            • @Joshcodes can you say more about the conflict resolution process? In this case, if the username already exists is the client expected to prompt the end user for a different username? What if the client is actually trying to use POST to change the username? Should PUT requests still be used for updating parameters, while POST is used for creating objects whether it be one at a time or several? Thanks.

              – BFar
              Jan 22 '15 at 22:41











            • @BFar2 if the username already exists then the client should prompt the user. To change the username, assuming the username is part of an already created resource which needs modified, PUT would be used because you are correct, POST is used for create, always and PUT for updates.

              – Joshcodes
              Jan 23 '15 at 1:09











            • explaining things using short and effective language is also a desirable skill

              – Junchen Liu
              Aug 24 '15 at 16:27
















            56














            The decision of whether to use PUT or POST to create a resource on a server with an HTTP + REST API is based on who owns the URL structure. Having the client know, or participate in defining, the URL struct is an unnecessary coupling akin to the undesirable couplings that arose from SOA. Escaping types of couplings is the reason REST is so popular. Therefore, the proper method to use is POST. There are exceptions to this rule and they occur when the client wishes to retain control over the location structure of the resources it deploys. This is rare and likely means something else is wrong.



            At this point some people will argue that if RESTful-URL's are used, the client does knows the URL of the resource and therefore a PUT is acceptable. After all, this is why canonical, normalized, Ruby on Rails, Django URLs are important, look at the Twitter API … blah blah blah. Those people need to understand there is no such thing as a Restful-URL and that Roy Fielding himself states that:




            A REST API must not define fixed resource names or hierarchies (an
            obvious coupling of client and server). Servers must have the freedom
            to control their own namespace. Instead, allow servers to instruct
            clients on how to construct appropriate URIs, such as is done in HTML
            forms and URI templates, by defining those instructions within media
            types and link relations. [Failure here implies that clients are
            assuming a resource structure due to out-of band information, such as
            a domain-specific standard, which is the data-oriented equivalent to
            RPC's functional coupling].



            http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven




            The idea of a RESTful-URL is actually a violation of REST as the server is in charge of the URL structure and should be free to decide how to use it to avoid coupling. If this confuses you read about the significance of self discovery on API design.



            Using POST to create resources comes with a design consideration because POST is not idempotent. This means that repeating a POST several times does not guarantee the same behavior each time. This scares people into using PUT to create resources when they should not. They know it's wrong (POST is for CREATE) but they do it anyway because they don't know how to solve this problem. This concern is demonstrated in the following situation:




            1. The client POST a new resource to the server.

            2. The server processes the request and sends a response.

            3. The client never receives the response.

            4. The server is unaware the client has not received the response.

            5. The client does not have a URL for the resource (therefore PUT is not an option) and repeats the POST.

            6. POST is not idempotent and the server …


            Step 6 is where people commonly get confused about what to do. However, there is no reason to create a kludge to solve this issue. Instead, HTTP can be used as specified in RFC 2616 and the server replies:




            10.4.10 409 Conflict



            The request could not be completed due to a conflict with the current
            state of the resource. This code is only allowed in situations where
            it is expected that the user might be able to resolve the conflict and
            resubmit the request. The response body SHOULD include enough



            information for the user to recognize the source of the conflict.
            Ideally, the response entity would include enough information for the
            user or user agent to fix the problem; however, that might not be
            possible and is not required.



            Conflicts are most likely to occur in response to a PUT request. For
            example, if versioning were being used and the entity being PUT
            included changes to a resource which conflict with those made by an
            earlier (third-party) request, the server might use the 409 response
            to indicate that it can’t complete the request. In this case, the
            response entity would likely contain a list of the differences between
            the two versions in a format defined by the response Content-Type.




            Replying with a status code of 409 Conflict is the correct recourse because:




            • Performing a POST of data which has an ID which matches a resource already in the system is “a conflict with the current state of the resource.”

            • Since the important part is for the client to understand the server has the resource and to take appropriate action. This is a “situation(s) where it is expected that the user might be able to resolve the conflict and resubmit the request.”

            • A response which contains the URL of the resource with the conflicting ID and the appropriate preconditions for the resource would provide “enough information for the user or user agent to fix the problem” which is the ideal case per RFC 2616.


            Update based on release of RFC 7231 to Replace 2616



            RFC 7231 is designed to replace 2616 and in Section 4.3.3 describes the follow possible response for a POST




            If the result of processing a POST would be equivalent to a
            representation of an existing resource, an origin server MAY redirect
            the user agent to that resource by sending a 303 (See Other) response
            with the existing resource's identifier in the Location field. This
            has the benefits of providing the user agent a resource identifier
            and transferring the representation via a method more amenable to
            shared caching, though at the cost of an extra request if the user
            agent does not already have the representation cached.




            It now may be tempting to simply return a 303 in the event that a POST is repeated. However, the opposite is true. Returning a 303 would only make sense if multiple create requests (creating different resources) return the same content. An example would be a "thank you for submitting your request message" that the client need not re-download each time. RFC 7231 still maintains in section 4.2.2 that POST is not to be idempotent and continues to maintain that POST should be used for create.



            For more information about this, read this article.






            share|improve this answer


























            • Would a 409 Conflict response be the appropriate code for something like trying to create a new account with a username that already exists? I've been using 409 for versioning conflicts specifically, but after reading your answer, I wonder if it shouldn't be used for any "duplicate" requests.

              – Eric B.
              Jul 9 '14 at 4:43











            • @EricB. Yes, in the situation you describe "due to a conflict with the current state of the resource" the operation fails. Additionally, it is reasonable to expect that the user can resolve the conflict and the message body only needs to inform the user that the username already exists.

              – Joshcodes
              Jul 10 '14 at 13:25











            • @Joshcodes can you say more about the conflict resolution process? In this case, if the username already exists is the client expected to prompt the end user for a different username? What if the client is actually trying to use POST to change the username? Should PUT requests still be used for updating parameters, while POST is used for creating objects whether it be one at a time or several? Thanks.

              – BFar
              Jan 22 '15 at 22:41











            • @BFar2 if the username already exists then the client should prompt the user. To change the username, assuming the username is part of an already created resource which needs modified, PUT would be used because you are correct, POST is used for create, always and PUT for updates.

              – Joshcodes
              Jan 23 '15 at 1:09











            • explaining things using short and effective language is also a desirable skill

              – Junchen Liu
              Aug 24 '15 at 16:27














            56












            56








            56







            The decision of whether to use PUT or POST to create a resource on a server with an HTTP + REST API is based on who owns the URL structure. Having the client know, or participate in defining, the URL struct is an unnecessary coupling akin to the undesirable couplings that arose from SOA. Escaping types of couplings is the reason REST is so popular. Therefore, the proper method to use is POST. There are exceptions to this rule and they occur when the client wishes to retain control over the location structure of the resources it deploys. This is rare and likely means something else is wrong.



            At this point some people will argue that if RESTful-URL's are used, the client does knows the URL of the resource and therefore a PUT is acceptable. After all, this is why canonical, normalized, Ruby on Rails, Django URLs are important, look at the Twitter API … blah blah blah. Those people need to understand there is no such thing as a Restful-URL and that Roy Fielding himself states that:




            A REST API must not define fixed resource names or hierarchies (an
            obvious coupling of client and server). Servers must have the freedom
            to control their own namespace. Instead, allow servers to instruct
            clients on how to construct appropriate URIs, such as is done in HTML
            forms and URI templates, by defining those instructions within media
            types and link relations. [Failure here implies that clients are
            assuming a resource structure due to out-of band information, such as
            a domain-specific standard, which is the data-oriented equivalent to
            RPC's functional coupling].



            http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven




            The idea of a RESTful-URL is actually a violation of REST as the server is in charge of the URL structure and should be free to decide how to use it to avoid coupling. If this confuses you read about the significance of self discovery on API design.



            Using POST to create resources comes with a design consideration because POST is not idempotent. This means that repeating a POST several times does not guarantee the same behavior each time. This scares people into using PUT to create resources when they should not. They know it's wrong (POST is for CREATE) but they do it anyway because they don't know how to solve this problem. This concern is demonstrated in the following situation:




            1. The client POST a new resource to the server.

            2. The server processes the request and sends a response.

            3. The client never receives the response.

            4. The server is unaware the client has not received the response.

            5. The client does not have a URL for the resource (therefore PUT is not an option) and repeats the POST.

            6. POST is not idempotent and the server …


            Step 6 is where people commonly get confused about what to do. However, there is no reason to create a kludge to solve this issue. Instead, HTTP can be used as specified in RFC 2616 and the server replies:




            10.4.10 409 Conflict



            The request could not be completed due to a conflict with the current
            state of the resource. This code is only allowed in situations where
            it is expected that the user might be able to resolve the conflict and
            resubmit the request. The response body SHOULD include enough



            information for the user to recognize the source of the conflict.
            Ideally, the response entity would include enough information for the
            user or user agent to fix the problem; however, that might not be
            possible and is not required.



            Conflicts are most likely to occur in response to a PUT request. For
            example, if versioning were being used and the entity being PUT
            included changes to a resource which conflict with those made by an
            earlier (third-party) request, the server might use the 409 response
            to indicate that it can’t complete the request. In this case, the
            response entity would likely contain a list of the differences between
            the two versions in a format defined by the response Content-Type.




            Replying with a status code of 409 Conflict is the correct recourse because:




            • Performing a POST of data which has an ID which matches a resource already in the system is “a conflict with the current state of the resource.”

            • Since the important part is for the client to understand the server has the resource and to take appropriate action. This is a “situation(s) where it is expected that the user might be able to resolve the conflict and resubmit the request.”

            • A response which contains the URL of the resource with the conflicting ID and the appropriate preconditions for the resource would provide “enough information for the user or user agent to fix the problem” which is the ideal case per RFC 2616.


            Update based on release of RFC 7231 to Replace 2616



            RFC 7231 is designed to replace 2616 and in Section 4.3.3 describes the follow possible response for a POST




            If the result of processing a POST would be equivalent to a
            representation of an existing resource, an origin server MAY redirect
            the user agent to that resource by sending a 303 (See Other) response
            with the existing resource's identifier in the Location field. This
            has the benefits of providing the user agent a resource identifier
            and transferring the representation via a method more amenable to
            shared caching, though at the cost of an extra request if the user
            agent does not already have the representation cached.




            It now may be tempting to simply return a 303 in the event that a POST is repeated. However, the opposite is true. Returning a 303 would only make sense if multiple create requests (creating different resources) return the same content. An example would be a "thank you for submitting your request message" that the client need not re-download each time. RFC 7231 still maintains in section 4.2.2 that POST is not to be idempotent and continues to maintain that POST should be used for create.



            For more information about this, read this article.






            share|improve this answer















            The decision of whether to use PUT or POST to create a resource on a server with an HTTP + REST API is based on who owns the URL structure. Having the client know, or participate in defining, the URL struct is an unnecessary coupling akin to the undesirable couplings that arose from SOA. Escaping types of couplings is the reason REST is so popular. Therefore, the proper method to use is POST. There are exceptions to this rule and they occur when the client wishes to retain control over the location structure of the resources it deploys. This is rare and likely means something else is wrong.



            At this point some people will argue that if RESTful-URL's are used, the client does knows the URL of the resource and therefore a PUT is acceptable. After all, this is why canonical, normalized, Ruby on Rails, Django URLs are important, look at the Twitter API … blah blah blah. Those people need to understand there is no such thing as a Restful-URL and that Roy Fielding himself states that:




            A REST API must not define fixed resource names or hierarchies (an
            obvious coupling of client and server). Servers must have the freedom
            to control their own namespace. Instead, allow servers to instruct
            clients on how to construct appropriate URIs, such as is done in HTML
            forms and URI templates, by defining those instructions within media
            types and link relations. [Failure here implies that clients are
            assuming a resource structure due to out-of band information, such as
            a domain-specific standard, which is the data-oriented equivalent to
            RPC's functional coupling].



            http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven




            The idea of a RESTful-URL is actually a violation of REST as the server is in charge of the URL structure and should be free to decide how to use it to avoid coupling. If this confuses you read about the significance of self discovery on API design.



            Using POST to create resources comes with a design consideration because POST is not idempotent. This means that repeating a POST several times does not guarantee the same behavior each time. This scares people into using PUT to create resources when they should not. They know it's wrong (POST is for CREATE) but they do it anyway because they don't know how to solve this problem. This concern is demonstrated in the following situation:




            1. The client POST a new resource to the server.

            2. The server processes the request and sends a response.

            3. The client never receives the response.

            4. The server is unaware the client has not received the response.

            5. The client does not have a URL for the resource (therefore PUT is not an option) and repeats the POST.

            6. POST is not idempotent and the server …


            Step 6 is where people commonly get confused about what to do. However, there is no reason to create a kludge to solve this issue. Instead, HTTP can be used as specified in RFC 2616 and the server replies:




            10.4.10 409 Conflict



            The request could not be completed due to a conflict with the current
            state of the resource. This code is only allowed in situations where
            it is expected that the user might be able to resolve the conflict and
            resubmit the request. The response body SHOULD include enough



            information for the user to recognize the source of the conflict.
            Ideally, the response entity would include enough information for the
            user or user agent to fix the problem; however, that might not be
            possible and is not required.



            Conflicts are most likely to occur in response to a PUT request. For
            example, if versioning were being used and the entity being PUT
            included changes to a resource which conflict with those made by an
            earlier (third-party) request, the server might use the 409 response
            to indicate that it can’t complete the request. In this case, the
            response entity would likely contain a list of the differences between
            the two versions in a format defined by the response Content-Type.




            Replying with a status code of 409 Conflict is the correct recourse because:




            • Performing a POST of data which has an ID which matches a resource already in the system is “a conflict with the current state of the resource.”

            • Since the important part is for the client to understand the server has the resource and to take appropriate action. This is a “situation(s) where it is expected that the user might be able to resolve the conflict and resubmit the request.”

            • A response which contains the URL of the resource with the conflicting ID and the appropriate preconditions for the resource would provide “enough information for the user or user agent to fix the problem” which is the ideal case per RFC 2616.


            Update based on release of RFC 7231 to Replace 2616



            RFC 7231 is designed to replace 2616 and in Section 4.3.3 describes the follow possible response for a POST




            If the result of processing a POST would be equivalent to a
            representation of an existing resource, an origin server MAY redirect
            the user agent to that resource by sending a 303 (See Other) response
            with the existing resource's identifier in the Location field. This
            has the benefits of providing the user agent a resource identifier
            and transferring the representation via a method more amenable to
            shared caching, though at the cost of an extra request if the user
            agent does not already have the representation cached.




            It now may be tempting to simply return a 303 in the event that a POST is repeated. However, the opposite is true. Returning a 303 would only make sense if multiple create requests (creating different resources) return the same content. An example would be a "thank you for submitting your request message" that the client need not re-download each time. RFC 7231 still maintains in section 4.2.2 that POST is not to be idempotent and continues to maintain that POST should be used for create.



            For more information about this, read this article.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Oct 21 '17 at 12:39









            Peter Mortensen

            13.7k1986111




            13.7k1986111










            answered Oct 29 '13 at 23:00









            JoshcodesJoshcodes

            5,49223336




            5,49223336













            • Would a 409 Conflict response be the appropriate code for something like trying to create a new account with a username that already exists? I've been using 409 for versioning conflicts specifically, but after reading your answer, I wonder if it shouldn't be used for any "duplicate" requests.

              – Eric B.
              Jul 9 '14 at 4:43











            • @EricB. Yes, in the situation you describe "due to a conflict with the current state of the resource" the operation fails. Additionally, it is reasonable to expect that the user can resolve the conflict and the message body only needs to inform the user that the username already exists.

              – Joshcodes
              Jul 10 '14 at 13:25











            • @Joshcodes can you say more about the conflict resolution process? In this case, if the username already exists is the client expected to prompt the end user for a different username? What if the client is actually trying to use POST to change the username? Should PUT requests still be used for updating parameters, while POST is used for creating objects whether it be one at a time or several? Thanks.

              – BFar
              Jan 22 '15 at 22:41











            • @BFar2 if the username already exists then the client should prompt the user. To change the username, assuming the username is part of an already created resource which needs modified, PUT would be used because you are correct, POST is used for create, always and PUT for updates.

              – Joshcodes
              Jan 23 '15 at 1:09











            • explaining things using short and effective language is also a desirable skill

              – Junchen Liu
              Aug 24 '15 at 16:27



















            • Would a 409 Conflict response be the appropriate code for something like trying to create a new account with a username that already exists? I've been using 409 for versioning conflicts specifically, but after reading your answer, I wonder if it shouldn't be used for any "duplicate" requests.

              – Eric B.
              Jul 9 '14 at 4:43











            • @EricB. Yes, in the situation you describe "due to a conflict with the current state of the resource" the operation fails. Additionally, it is reasonable to expect that the user can resolve the conflict and the message body only needs to inform the user that the username already exists.

              – Joshcodes
              Jul 10 '14 at 13:25











            • @Joshcodes can you say more about the conflict resolution process? In this case, if the username already exists is the client expected to prompt the end user for a different username? What if the client is actually trying to use POST to change the username? Should PUT requests still be used for updating parameters, while POST is used for creating objects whether it be one at a time or several? Thanks.

              – BFar
              Jan 22 '15 at 22:41











            • @BFar2 if the username already exists then the client should prompt the user. To change the username, assuming the username is part of an already created resource which needs modified, PUT would be used because you are correct, POST is used for create, always and PUT for updates.

              – Joshcodes
              Jan 23 '15 at 1:09











            • explaining things using short and effective language is also a desirable skill

              – Junchen Liu
              Aug 24 '15 at 16:27

















            Would a 409 Conflict response be the appropriate code for something like trying to create a new account with a username that already exists? I've been using 409 for versioning conflicts specifically, but after reading your answer, I wonder if it shouldn't be used for any "duplicate" requests.

            – Eric B.
            Jul 9 '14 at 4:43





            Would a 409 Conflict response be the appropriate code for something like trying to create a new account with a username that already exists? I've been using 409 for versioning conflicts specifically, but after reading your answer, I wonder if it shouldn't be used for any "duplicate" requests.

            – Eric B.
            Jul 9 '14 at 4:43













            @EricB. Yes, in the situation you describe "due to a conflict with the current state of the resource" the operation fails. Additionally, it is reasonable to expect that the user can resolve the conflict and the message body only needs to inform the user that the username already exists.

            – Joshcodes
            Jul 10 '14 at 13:25





            @EricB. Yes, in the situation you describe "due to a conflict with the current state of the resource" the operation fails. Additionally, it is reasonable to expect that the user can resolve the conflict and the message body only needs to inform the user that the username already exists.

            – Joshcodes
            Jul 10 '14 at 13:25













            @Joshcodes can you say more about the conflict resolution process? In this case, if the username already exists is the client expected to prompt the end user for a different username? What if the client is actually trying to use POST to change the username? Should PUT requests still be used for updating parameters, while POST is used for creating objects whether it be one at a time or several? Thanks.

            – BFar
            Jan 22 '15 at 22:41





            @Joshcodes can you say more about the conflict resolution process? In this case, if the username already exists is the client expected to prompt the end user for a different username? What if the client is actually trying to use POST to change the username? Should PUT requests still be used for updating parameters, while POST is used for creating objects whether it be one at a time or several? Thanks.

            – BFar
            Jan 22 '15 at 22:41













            @BFar2 if the username already exists then the client should prompt the user. To change the username, assuming the username is part of an already created resource which needs modified, PUT would be used because you are correct, POST is used for create, always and PUT for updates.

            – Joshcodes
            Jan 23 '15 at 1:09





            @BFar2 if the username already exists then the client should prompt the user. To change the username, assuming the username is part of an already created resource which needs modified, PUT would be used because you are correct, POST is used for create, always and PUT for updates.

            – Joshcodes
            Jan 23 '15 at 1:09













            explaining things using short and effective language is also a desirable skill

            – Junchen Liu
            Aug 24 '15 at 16:27





            explaining things using short and effective language is also a desirable skill

            – Junchen Liu
            Aug 24 '15 at 16:27











            51














            I like this advice, from RFC 2616's definition of 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.




            This jibes with the other advice here, that PUT is best applied to resources that already have a name, and POST is good for creating a new object under an existing resource (and letting the server name it).



            I interpret this, and the idempotency requirements on PUT, to mean that:




            • POST is good for creating new objects under a collection (and create does not need to be idempotent)

            • PUT is good for updating existing objects (and update needs to be idempotent)

            • POST can also be used for non-idempotent updates to existing objects (especially, changing part of an object without specifying the whole thing -- if you think about it, creating a new member of a collection is actually a special case of this kind of update, from the collection's perspective)

            • PUT can also be used for create if and only if you allow the client to name the resource. But since REST clients aren't supposed to make assumptions about URL structure, this is less in the intended spirit of things.






            share|improve this answer



















            • 3





              "POST can also be used for non-idempotent updates to existing objects (especially, changing part of an object without specifying the whole thing" That's what PATCH is for

              – Snuggs
              May 4 '12 at 22:11


















            51














            I like this advice, from RFC 2616's definition of 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.




            This jibes with the other advice here, that PUT is best applied to resources that already have a name, and POST is good for creating a new object under an existing resource (and letting the server name it).



            I interpret this, and the idempotency requirements on PUT, to mean that:




            • POST is good for creating new objects under a collection (and create does not need to be idempotent)

            • PUT is good for updating existing objects (and update needs to be idempotent)

            • POST can also be used for non-idempotent updates to existing objects (especially, changing part of an object without specifying the whole thing -- if you think about it, creating a new member of a collection is actually a special case of this kind of update, from the collection's perspective)

            • PUT can also be used for create if and only if you allow the client to name the resource. But since REST clients aren't supposed to make assumptions about URL structure, this is less in the intended spirit of things.






            share|improve this answer



















            • 3





              "POST can also be used for non-idempotent updates to existing objects (especially, changing part of an object without specifying the whole thing" That's what PATCH is for

              – Snuggs
              May 4 '12 at 22:11
















            51












            51








            51







            I like this advice, from RFC 2616's definition of 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.




            This jibes with the other advice here, that PUT is best applied to resources that already have a name, and POST is good for creating a new object under an existing resource (and letting the server name it).



            I interpret this, and the idempotency requirements on PUT, to mean that:




            • POST is good for creating new objects under a collection (and create does not need to be idempotent)

            • PUT is good for updating existing objects (and update needs to be idempotent)

            • POST can also be used for non-idempotent updates to existing objects (especially, changing part of an object without specifying the whole thing -- if you think about it, creating a new member of a collection is actually a special case of this kind of update, from the collection's perspective)

            • PUT can also be used for create if and only if you allow the client to name the resource. But since REST clients aren't supposed to make assumptions about URL structure, this is less in the intended spirit of things.






            share|improve this answer













            I like this advice, from RFC 2616's definition of 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.




            This jibes with the other advice here, that PUT is best applied to resources that already have a name, and POST is good for creating a new object under an existing resource (and letting the server name it).



            I interpret this, and the idempotency requirements on PUT, to mean that:




            • POST is good for creating new objects under a collection (and create does not need to be idempotent)

            • PUT is good for updating existing objects (and update needs to be idempotent)

            • POST can also be used for non-idempotent updates to existing objects (especially, changing part of an object without specifying the whole thing -- if you think about it, creating a new member of a collection is actually a special case of this kind of update, from the collection's perspective)

            • PUT can also be used for create if and only if you allow the client to name the resource. But since REST clients aren't supposed to make assumptions about URL structure, this is less in the intended spirit of things.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 11 '12 at 17:18









            metamattmetamatt

            8,57163551




            8,57163551








            • 3





              "POST can also be used for non-idempotent updates to existing objects (especially, changing part of an object without specifying the whole thing" That's what PATCH is for

              – Snuggs
              May 4 '12 at 22:11
















            • 3





              "POST can also be used for non-idempotent updates to existing objects (especially, changing part of an object without specifying the whole thing" That's what PATCH is for

              – Snuggs
              May 4 '12 at 22:11










            3




            3





            "POST can also be used for non-idempotent updates to existing objects (especially, changing part of an object without specifying the whole thing" That's what PATCH is for

            – Snuggs
            May 4 '12 at 22:11







            "POST can also be used for non-idempotent updates to existing objects (especially, changing part of an object without specifying the whole thing" That's what PATCH is for

            – Snuggs
            May 4 '12 at 22:11













            43














            In short:



            PUT is idempotent, where the resource state will be the same if the same operation is executed one time or multiple times.



            POST is non-idempotent, where the resource state may become different if the operation is executed multiple times as compared to executing a single time.



            Analogy with database query



            PUT You can think of similar to "UPDATE STUDENT SET address = "abc" where id="123";



            POST You can think of something like "INSERT INTO STUDENT(name, address) VALUES ("abc", "xyzzz");



            Student Id is auto generated.



            With PUT, if the same query is executed multiple times or one time, the STUDENT table state remains the same.



            In case of POST, if the same query is executed multiple times then multiple Student records get created in the database and the database state changes on each execution of an "INSERT" query.



            NOTE: PUT needs a resource location (already-resource) on which update needs to happen, whereas POST doesn't require that. Therefore intuitively POST is meant for creation of a new resource, whereas PUT is needed for updating the already existing resource.



            Some may come up with that updates can be performed with POST. There is no hard rule which one to use for updates or which one to use for create. Again these are conventions, and intuitively I'm inclined with the above mentioned reasoning and follow it.






            share|improve this answer





















            • 5





              for PUT is similar to INSERT or UPDATE query

              – Eugen Konkov
              Aug 22 '16 at 9:25






            • 1





              actually PUT You can think of similar to "UPDATE STUDENT SET address = "abc" where id="123"; would be a statement for PATCH. "UPDATE STUDENT SET address = "abc", name="newname" where id="123" would be a correct analogy for PUT

              – mko
              Apr 12 '17 at 13:24











            • Put could also be used for INSERT. For example if you server detected you were trying to upload the same file multiple times, it would make your request idempotent. (No new file uploads are done).

              – kiwicomb123
              Aug 20 '18 at 22:46
















            43














            In short:



            PUT is idempotent, where the resource state will be the same if the same operation is executed one time or multiple times.



            POST is non-idempotent, where the resource state may become different if the operation is executed multiple times as compared to executing a single time.



            Analogy with database query



            PUT You can think of similar to "UPDATE STUDENT SET address = "abc" where id="123";



            POST You can think of something like "INSERT INTO STUDENT(name, address) VALUES ("abc", "xyzzz");



            Student Id is auto generated.



            With PUT, if the same query is executed multiple times or one time, the STUDENT table state remains the same.



            In case of POST, if the same query is executed multiple times then multiple Student records get created in the database and the database state changes on each execution of an "INSERT" query.



            NOTE: PUT needs a resource location (already-resource) on which update needs to happen, whereas POST doesn't require that. Therefore intuitively POST is meant for creation of a new resource, whereas PUT is needed for updating the already existing resource.



            Some may come up with that updates can be performed with POST. There is no hard rule which one to use for updates or which one to use for create. Again these are conventions, and intuitively I'm inclined with the above mentioned reasoning and follow it.






            share|improve this answer





















            • 5





              for PUT is similar to INSERT or UPDATE query

              – Eugen Konkov
              Aug 22 '16 at 9:25






            • 1





              actually PUT You can think of similar to "UPDATE STUDENT SET address = "abc" where id="123"; would be a statement for PATCH. "UPDATE STUDENT SET address = "abc", name="newname" where id="123" would be a correct analogy for PUT

              – mko
              Apr 12 '17 at 13:24











            • Put could also be used for INSERT. For example if you server detected you were trying to upload the same file multiple times, it would make your request idempotent. (No new file uploads are done).

              – kiwicomb123
              Aug 20 '18 at 22:46














            43












            43








            43







            In short:



            PUT is idempotent, where the resource state will be the same if the same operation is executed one time or multiple times.



            POST is non-idempotent, where the resource state may become different if the operation is executed multiple times as compared to executing a single time.



            Analogy with database query



            PUT You can think of similar to "UPDATE STUDENT SET address = "abc" where id="123";



            POST You can think of something like "INSERT INTO STUDENT(name, address) VALUES ("abc", "xyzzz");



            Student Id is auto generated.



            With PUT, if the same query is executed multiple times or one time, the STUDENT table state remains the same.



            In case of POST, if the same query is executed multiple times then multiple Student records get created in the database and the database state changes on each execution of an "INSERT" query.



            NOTE: PUT needs a resource location (already-resource) on which update needs to happen, whereas POST doesn't require that. Therefore intuitively POST is meant for creation of a new resource, whereas PUT is needed for updating the already existing resource.



            Some may come up with that updates can be performed with POST. There is no hard rule which one to use for updates or which one to use for create. Again these are conventions, and intuitively I'm inclined with the above mentioned reasoning and follow it.






            share|improve this answer















            In short:



            PUT is idempotent, where the resource state will be the same if the same operation is executed one time or multiple times.



            POST is non-idempotent, where the resource state may become different if the operation is executed multiple times as compared to executing a single time.



            Analogy with database query



            PUT You can think of similar to "UPDATE STUDENT SET address = "abc" where id="123";



            POST You can think of something like "INSERT INTO STUDENT(name, address) VALUES ("abc", "xyzzz");



            Student Id is auto generated.



            With PUT, if the same query is executed multiple times or one time, the STUDENT table state remains the same.



            In case of POST, if the same query is executed multiple times then multiple Student records get created in the database and the database state changes on each execution of an "INSERT" query.



            NOTE: PUT needs a resource location (already-resource) on which update needs to happen, whereas POST doesn't require that. Therefore intuitively POST is meant for creation of a new resource, whereas PUT is needed for updating the already existing resource.



            Some may come up with that updates can be performed with POST. There is no hard rule which one to use for updates or which one to use for create. Again these are conventions, and intuitively I'm inclined with the above mentioned reasoning and follow it.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Oct 21 '17 at 12:46









            Peter Mortensen

            13.7k1986111




            13.7k1986111










            answered Jul 29 '16 at 11:11









            bharatjbharatj

            1,28311423




            1,28311423








            • 5





              for PUT is similar to INSERT or UPDATE query

              – Eugen Konkov
              Aug 22 '16 at 9:25






            • 1





              actually PUT You can think of similar to "UPDATE STUDENT SET address = "abc" where id="123"; would be a statement for PATCH. "UPDATE STUDENT SET address = "abc", name="newname" where id="123" would be a correct analogy for PUT

              – mko
              Apr 12 '17 at 13:24











            • Put could also be used for INSERT. For example if you server detected you were trying to upload the same file multiple times, it would make your request idempotent. (No new file uploads are done).

              – kiwicomb123
              Aug 20 '18 at 22:46














            • 5





              for PUT is similar to INSERT or UPDATE query

              – Eugen Konkov
              Aug 22 '16 at 9:25






            • 1





              actually PUT You can think of similar to "UPDATE STUDENT SET address = "abc" where id="123"; would be a statement for PATCH. "UPDATE STUDENT SET address = "abc", name="newname" where id="123" would be a correct analogy for PUT

              – mko
              Apr 12 '17 at 13:24











            • Put could also be used for INSERT. For example if you server detected you were trying to upload the same file multiple times, it would make your request idempotent. (No new file uploads are done).

              – kiwicomb123
              Aug 20 '18 at 22:46








            5




            5





            for PUT is similar to INSERT or UPDATE query

            – Eugen Konkov
            Aug 22 '16 at 9:25





            for PUT is similar to INSERT or UPDATE query

            – Eugen Konkov
            Aug 22 '16 at 9:25




            1




            1





            actually PUT You can think of similar to "UPDATE STUDENT SET address = "abc" where id="123"; would be a statement for PATCH. "UPDATE STUDENT SET address = "abc", name="newname" where id="123" would be a correct analogy for PUT

            – mko
            Apr 12 '17 at 13:24





            actually PUT You can think of similar to "UPDATE STUDENT SET address = "abc" where id="123"; would be a statement for PATCH. "UPDATE STUDENT SET address = "abc", name="newname" where id="123" would be a correct analogy for PUT

            – mko
            Apr 12 '17 at 13:24













            Put could also be used for INSERT. For example if you server detected you were trying to upload the same file multiple times, it would make your request idempotent. (No new file uploads are done).

            – kiwicomb123
            Aug 20 '18 at 22:46





            Put could also be used for INSERT. For example if you server detected you were trying to upload the same file multiple times, it would make your request idempotent. (No new file uploads are done).

            – kiwicomb123
            Aug 20 '18 at 22:46











            41














            POST is like posting a letter to a mailbox or posting an email to an email queue.
            PUT is like when you put an object in a cubby hole or a place on a shelf (it has a known address).



            With POST, you're posting to the address of the QUEUE or COLLECTION. With PUT, you're putting to the address of the ITEM.



            PUT is idempotent. You can send the request 100 times and it will not matter. POST is not idempotent. If you send the request 100 times, you'll get 100 emails or 100 letters in your postal box.



            A general rule: if you know the id or name of the item, use PUT. If you want the id or name of the item to be assigned by the receiving party, use POST.



            POST versus PUT






            share|improve this answer





















            • 1





              No, PUT implies that you know the URL. If you only know the ID then POST with that ID to get the URL.

              – Joshcodes
              Oct 29 '13 at 17:35






            • 5





              The id is part of the URL, so yes, use PUT if you know the URL (which includes the id).

              – Homer6
              Oct 29 '13 at 22:02











            • No, the URL is determined by the server and the ID is not necessarily part of the URL. Roy Fielding would tell you the same or you could just read his thesis.

              – Joshcodes
              Oct 29 '13 at 23:06











            • @Joshcodes, is that assuming REST? In a RESTful architecture, the item id is most definitely part of the URL, as in: /people/123. I like this site for REST: microformats.org/wiki/rest/urls

              – Beez
              Dec 26 '13 at 19:10






            • 1





              @Beez the mircoformats link suggests a good way for servers to structure their URLs but the server determines the URL. The client next-to-never does. See my answer or associated article if you don't understand this.

              – Joshcodes
              Jan 7 '14 at 17:11


















            41














            POST is like posting a letter to a mailbox or posting an email to an email queue.
            PUT is like when you put an object in a cubby hole or a place on a shelf (it has a known address).



            With POST, you're posting to the address of the QUEUE or COLLECTION. With PUT, you're putting to the address of the ITEM.



            PUT is idempotent. You can send the request 100 times and it will not matter. POST is not idempotent. If you send the request 100 times, you'll get 100 emails or 100 letters in your postal box.



            A general rule: if you know the id or name of the item, use PUT. If you want the id or name of the item to be assigned by the receiving party, use POST.



            POST versus PUT






            share|improve this answer





















            • 1





              No, PUT implies that you know the URL. If you only know the ID then POST with that ID to get the URL.

              – Joshcodes
              Oct 29 '13 at 17:35






            • 5





              The id is part of the URL, so yes, use PUT if you know the URL (which includes the id).

              – Homer6
              Oct 29 '13 at 22:02











            • No, the URL is determined by the server and the ID is not necessarily part of the URL. Roy Fielding would tell you the same or you could just read his thesis.

              – Joshcodes
              Oct 29 '13 at 23:06











            • @Joshcodes, is that assuming REST? In a RESTful architecture, the item id is most definitely part of the URL, as in: /people/123. I like this site for REST: microformats.org/wiki/rest/urls

              – Beez
              Dec 26 '13 at 19:10






            • 1





              @Beez the mircoformats link suggests a good way for servers to structure their URLs but the server determines the URL. The client next-to-never does. See my answer or associated article if you don't understand this.

              – Joshcodes
              Jan 7 '14 at 17:11
















            41












            41








            41







            POST is like posting a letter to a mailbox or posting an email to an email queue.
            PUT is like when you put an object in a cubby hole or a place on a shelf (it has a known address).



            With POST, you're posting to the address of the QUEUE or COLLECTION. With PUT, you're putting to the address of the ITEM.



            PUT is idempotent. You can send the request 100 times and it will not matter. POST is not idempotent. If you send the request 100 times, you'll get 100 emails or 100 letters in your postal box.



            A general rule: if you know the id or name of the item, use PUT. If you want the id or name of the item to be assigned by the receiving party, use POST.



            POST versus PUT






            share|improve this answer















            POST is like posting a letter to a mailbox or posting an email to an email queue.
            PUT is like when you put an object in a cubby hole or a place on a shelf (it has a known address).



            With POST, you're posting to the address of the QUEUE or COLLECTION. With PUT, you're putting to the address of the ITEM.



            PUT is idempotent. You can send the request 100 times and it will not matter. POST is not idempotent. If you send the request 100 times, you'll get 100 emails or 100 letters in your postal box.



            A general rule: if you know the id or name of the item, use PUT. If you want the id or name of the item to be assigned by the receiving party, use POST.



            POST versus PUT







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jul 10 '13 at 19:52

























            answered Jun 14 '13 at 18:10









            Homer6Homer6

            9,47984869




            9,47984869








            • 1





              No, PUT implies that you know the URL. If you only know the ID then POST with that ID to get the URL.

              – Joshcodes
              Oct 29 '13 at 17:35






            • 5





              The id is part of the URL, so yes, use PUT if you know the URL (which includes the id).

              – Homer6
              Oct 29 '13 at 22:02











            • No, the URL is determined by the server and the ID is not necessarily part of the URL. Roy Fielding would tell you the same or you could just read his thesis.

              – Joshcodes
              Oct 29 '13 at 23:06











            • @Joshcodes, is that assuming REST? In a RESTful architecture, the item id is most definitely part of the URL, as in: /people/123. I like this site for REST: microformats.org/wiki/rest/urls

              – Beez
              Dec 26 '13 at 19:10






            • 1





              @Beez the mircoformats link suggests a good way for servers to structure their URLs but the server determines the URL. The client next-to-never does. See my answer or associated article if you don't understand this.

              – Joshcodes
              Jan 7 '14 at 17:11
















            • 1





              No, PUT implies that you know the URL. If you only know the ID then POST with that ID to get the URL.

              – Joshcodes
              Oct 29 '13 at 17:35






            • 5





              The id is part of the URL, so yes, use PUT if you know the URL (which includes the id).

              – Homer6
              Oct 29 '13 at 22:02











            • No, the URL is determined by the server and the ID is not necessarily part of the URL. Roy Fielding would tell you the same or you could just read his thesis.

              – Joshcodes
              Oct 29 '13 at 23:06











            • @Joshcodes, is that assuming REST? In a RESTful architecture, the item id is most definitely part of the URL, as in: /people/123. I like this site for REST: microformats.org/wiki/rest/urls

              – Beez
              Dec 26 '13 at 19:10






            • 1





              @Beez the mircoformats link suggests a good way for servers to structure their URLs but the server determines the URL. The client next-to-never does. See my answer or associated article if you don't understand this.

              – Joshcodes
              Jan 7 '14 at 17:11










            1




            1





            No, PUT implies that you know the URL. If you only know the ID then POST with that ID to get the URL.

            – Joshcodes
            Oct 29 '13 at 17:35





            No, PUT implies that you know the URL. If you only know the ID then POST with that ID to get the URL.

            – Joshcodes
            Oct 29 '13 at 17:35




            5




            5





            The id is part of the URL, so yes, use PUT if you know the URL (which includes the id).

            – Homer6
            Oct 29 '13 at 22:02





            The id is part of the URL, so yes, use PUT if you know the URL (which includes the id).

            – Homer6
            Oct 29 '13 at 22:02













            No, the URL is determined by the server and the ID is not necessarily part of the URL. Roy Fielding would tell you the same or you could just read his thesis.

            – Joshcodes
            Oct 29 '13 at 23:06





            No, the URL is determined by the server and the ID is not necessarily part of the URL. Roy Fielding would tell you the same or you could just read his thesis.

            – Joshcodes
            Oct 29 '13 at 23:06













            @Joshcodes, is that assuming REST? In a RESTful architecture, the item id is most definitely part of the URL, as in: /people/123. I like this site for REST: microformats.org/wiki/rest/urls

            – Beez
            Dec 26 '13 at 19:10





            @Joshcodes, is that assuming REST? In a RESTful architecture, the item id is most definitely part of the URL, as in: /people/123. I like this site for REST: microformats.org/wiki/rest/urls

            – Beez
            Dec 26 '13 at 19:10




            1




            1





            @Beez the mircoformats link suggests a good way for servers to structure their URLs but the server determines the URL. The client next-to-never does. See my answer or associated article if you don't understand this.

            – Joshcodes
            Jan 7 '14 at 17:11







            @Beez the mircoformats link suggests a good way for servers to structure their URLs but the server determines the URL. The client next-to-never does. See my answer or associated article if you don't understand this.

            – Joshcodes
            Jan 7 '14 at 17:11













            37














            New answer (now that I understand REST better):



            PUT is merely a statement of what content the service should, from now on, use to render representations of the resource identified by the client; POST is a statement of what content the service should, from now on, contain (possibly duplicated) but it's up to the server how to identify that content.



            PUT x (if x identifies a resource): "Replace the content of the resource identified by x with my content."



            PUT x (if x does not identify a resource): "Create a new resource containing my content and use x to identify it."



            POST x: "Store my content and give me an identifier that I can use to identify a resource (old or new) containing said content (possibly mixed with other content). Said resource should be identical or subordinate to that which x identifies." "y's resource is subordinate to x's resource" is typically but not necessarily implemented by making y a subpath of x (e.g. x = /foo and y = /foo/bar) and modifying the representation(s) of x's resource to reflect the existence of a new resource, e.g. with a hyperlink to y's resource and some metadata. Only the latter is really essential to good design, as URLs are opaque in REST -- you're supposed to use hypermedia instead of client-side URL construction to traverse the service anyways.



            In REST, there's no such thing as a resource containing "content". I refer as "content" to data that the service uses to render representations consistently. It typically consists of some related rows in a database or a file (e.g. an image file). It's up to the service to convert the user's content into something the service can use, e.g. converting a JSON payload into SQL statements.



            Original answer (might be easier to read):



            PUT /something (if /something already exists): "Take whatever you have at /something and replace it with what I give you."



            PUT /something (if /something does not already exist): "Take what I give you and put it at /something."



            POST /something: "Take what I give you and put it anywhere you want under /something as long as you give me its URL when you're done."






            share|improve this answer


























            • But how can you use PUT to create a new resource if it doesn't exist, while your ID generation method is on Auto Increment ? Usually ORM's does auto generate the ID for you, like the way you want it to be in a POST for example. Does it mean that if you want to implement PUT the right way you have to change your id auto generation ? This is awkward if the answer is yes.

              – Roni Axelrad
              Sep 16 '18 at 15:28






            • 1





              @RoniAxelrad : PUT is like a database "INSERT OR UPDATE" statement where you are including the key in the statement, so only applicable where you can guarente no collisions. eg. your domain has a 'natural key' or you use a guid. POST is like inserting into a table with an auto incrementing key. You have to be told by the database what ID it got after it has been inserted. Note your "INSERT OR UPDATE" will replace any previous data if it existed.

              – Nigel Thorne
              Nov 26 '18 at 1:33













            • @NigelThorne Thanks for your answer. So if for example I'm trying to PUT a book id 10 with a URI: PUT books/10. If book id 10 does not exists, I should create a book with id 10 right? but I cannot control the creation ID numerator, because it's auto increment. what should I do in that situation ?

              – Roni Axelrad
              Nov 27 '18 at 20:50






            • 1





              @RoniAxelrad REST PUT to an ID that doesn't exist is a request to the server to create a resource. It's still up to the server to decide if it wants to allow that. The server is in charge. It can respond with "No. I'm not going to do that". You already do that if the user doesn't have enough permissions...etc. It's okay for the server to say "No". REST is a convention that lets us define the meaning of various types of request ... your server decides what to do with those requests based on your business logic :) Even if it says "no" it's still following REST :)

              – Nigel Thorne
              Dec 3 '18 at 7:03


















            37














            New answer (now that I understand REST better):



            PUT is merely a statement of what content the service should, from now on, use to render representations of the resource identified by the client; POST is a statement of what content the service should, from now on, contain (possibly duplicated) but it's up to the server how to identify that content.



            PUT x (if x identifies a resource): "Replace the content of the resource identified by x with my content."



            PUT x (if x does not identify a resource): "Create a new resource containing my content and use x to identify it."



            POST x: "Store my content and give me an identifier that I can use to identify a resource (old or new) containing said content (possibly mixed with other content). Said resource should be identical or subordinate to that which x identifies." "y's resource is subordinate to x's resource" is typically but not necessarily implemented by making y a subpath of x (e.g. x = /foo and y = /foo/bar) and modifying the representation(s) of x's resource to reflect the existence of a new resource, e.g. with a hyperlink to y's resource and some metadata. Only the latter is really essential to good design, as URLs are opaque in REST -- you're supposed to use hypermedia instead of client-side URL construction to traverse the service anyways.



            In REST, there's no such thing as a resource containing "content". I refer as "content" to data that the service uses to render representations consistently. It typically consists of some related rows in a database or a file (e.g. an image file). It's up to the service to convert the user's content into something the service can use, e.g. converting a JSON payload into SQL statements.



            Original answer (might be easier to read):



            PUT /something (if /something already exists): "Take whatever you have at /something and replace it with what I give you."



            PUT /something (if /something does not already exist): "Take what I give you and put it at /something."



            POST /something: "Take what I give you and put it anywhere you want under /something as long as you give me its URL when you're done."






            share|improve this answer


























            • But how can you use PUT to create a new resource if it doesn't exist, while your ID generation method is on Auto Increment ? Usually ORM's does auto generate the ID for you, like the way you want it to be in a POST for example. Does it mean that if you want to implement PUT the right way you have to change your id auto generation ? This is awkward if the answer is yes.

              – Roni Axelrad
              Sep 16 '18 at 15:28






            • 1





              @RoniAxelrad : PUT is like a database "INSERT OR UPDATE" statement where you are including the key in the statement, so only applicable where you can guarente no collisions. eg. your domain has a 'natural key' or you use a guid. POST is like inserting into a table with an auto incrementing key. You have to be told by the database what ID it got after it has been inserted. Note your "INSERT OR UPDATE" will replace any previous data if it existed.

              – Nigel Thorne
              Nov 26 '18 at 1:33













            • @NigelThorne Thanks for your answer. So if for example I'm trying to PUT a book id 10 with a URI: PUT books/10. If book id 10 does not exists, I should create a book with id 10 right? but I cannot control the creation ID numerator, because it's auto increment. what should I do in that situation ?

              – Roni Axelrad
              Nov 27 '18 at 20:50






            • 1





              @RoniAxelrad REST PUT to an ID that doesn't exist is a request to the server to create a resource. It's still up to the server to decide if it wants to allow that. The server is in charge. It can respond with "No. I'm not going to do that". You already do that if the user doesn't have enough permissions...etc. It's okay for the server to say "No". REST is a convention that lets us define the meaning of various types of request ... your server decides what to do with those requests based on your business logic :) Even if it says "no" it's still following REST :)

              – Nigel Thorne
              Dec 3 '18 at 7:03
















            37












            37








            37







            New answer (now that I understand REST better):



            PUT is merely a statement of what content the service should, from now on, use to render representations of the resource identified by the client; POST is a statement of what content the service should, from now on, contain (possibly duplicated) but it's up to the server how to identify that content.



            PUT x (if x identifies a resource): "Replace the content of the resource identified by x with my content."



            PUT x (if x does not identify a resource): "Create a new resource containing my content and use x to identify it."



            POST x: "Store my content and give me an identifier that I can use to identify a resource (old or new) containing said content (possibly mixed with other content). Said resource should be identical or subordinate to that which x identifies." "y's resource is subordinate to x's resource" is typically but not necessarily implemented by making y a subpath of x (e.g. x = /foo and y = /foo/bar) and modifying the representation(s) of x's resource to reflect the existence of a new resource, e.g. with a hyperlink to y's resource and some metadata. Only the latter is really essential to good design, as URLs are opaque in REST -- you're supposed to use hypermedia instead of client-side URL construction to traverse the service anyways.



            In REST, there's no such thing as a resource containing "content". I refer as "content" to data that the service uses to render representations consistently. It typically consists of some related rows in a database or a file (e.g. an image file). It's up to the service to convert the user's content into something the service can use, e.g. converting a JSON payload into SQL statements.



            Original answer (might be easier to read):



            PUT /something (if /something already exists): "Take whatever you have at /something and replace it with what I give you."



            PUT /something (if /something does not already exist): "Take what I give you and put it at /something."



            POST /something: "Take what I give you and put it anywhere you want under /something as long as you give me its URL when you're done."






            share|improve this answer















            New answer (now that I understand REST better):



            PUT is merely a statement of what content the service should, from now on, use to render representations of the resource identified by the client; POST is a statement of what content the service should, from now on, contain (possibly duplicated) but it's up to the server how to identify that content.



            PUT x (if x identifies a resource): "Replace the content of the resource identified by x with my content."



            PUT x (if x does not identify a resource): "Create a new resource containing my content and use x to identify it."



            POST x: "Store my content and give me an identifier that I can use to identify a resource (old or new) containing said content (possibly mixed with other content). Said resource should be identical or subordinate to that which x identifies." "y's resource is subordinate to x's resource" is typically but not necessarily implemented by making y a subpath of x (e.g. x = /foo and y = /foo/bar) and modifying the representation(s) of x's resource to reflect the existence of a new resource, e.g. with a hyperlink to y's resource and some metadata. Only the latter is really essential to good design, as URLs are opaque in REST -- you're supposed to use hypermedia instead of client-side URL construction to traverse the service anyways.



            In REST, there's no such thing as a resource containing "content". I refer as "content" to data that the service uses to render representations consistently. It typically consists of some related rows in a database or a file (e.g. an image file). It's up to the service to convert the user's content into something the service can use, e.g. converting a JSON payload into SQL statements.



            Original answer (might be easier to read):



            PUT /something (if /something already exists): "Take whatever you have at /something and replace it with what I give you."



            PUT /something (if /something does not already exist): "Take what I give you and put it at /something."



            POST /something: "Take what I give you and put it anywhere you want under /something as long as you give me its URL when you're done."







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 23 '17 at 11:55









            Community

            11




            11










            answered Aug 1 '12 at 20:10









            JordanJordan

            2,44222338




            2,44222338













            • But how can you use PUT to create a new resource if it doesn't exist, while your ID generation method is on Auto Increment ? Usually ORM's does auto generate the ID for you, like the way you want it to be in a POST for example. Does it mean that if you want to implement PUT the right way you have to change your id auto generation ? This is awkward if the answer is yes.

              – Roni Axelrad
              Sep 16 '18 at 15:28






            • 1





              @RoniAxelrad : PUT is like a database "INSERT OR UPDATE" statement where you are including the key in the statement, so only applicable where you can guarente no collisions. eg. your domain has a 'natural key' or you use a guid. POST is like inserting into a table with an auto incrementing key. You have to be told by the database what ID it got after it has been inserted. Note your "INSERT OR UPDATE" will replace any previous data if it existed.

              – Nigel Thorne
              Nov 26 '18 at 1:33













            • @NigelThorne Thanks for your answer. So if for example I'm trying to PUT a book id 10 with a URI: PUT books/10. If book id 10 does not exists, I should create a book with id 10 right? but I cannot control the creation ID numerator, because it's auto increment. what should I do in that situation ?

              – Roni Axelrad
              Nov 27 '18 at 20:50






            • 1





              @RoniAxelrad REST PUT to an ID that doesn't exist is a request to the server to create a resource. It's still up to the server to decide if it wants to allow that. The server is in charge. It can respond with "No. I'm not going to do that". You already do that if the user doesn't have enough permissions...etc. It's okay for the server to say "No". REST is a convention that lets us define the meaning of various types of request ... your server decides what to do with those requests based on your business logic :) Even if it says "no" it's still following REST :)

              – Nigel Thorne
              Dec 3 '18 at 7:03





















            • But how can you use PUT to create a new resource if it doesn't exist, while your ID generation method is on Auto Increment ? Usually ORM's does auto generate the ID for you, like the way you want it to be in a POST for example. Does it mean that if you want to implement PUT the right way you have to change your id auto generation ? This is awkward if the answer is yes.

              – Roni Axelrad
              Sep 16 '18 at 15:28






            • 1





              @RoniAxelrad : PUT is like a database "INSERT OR UPDATE" statement where you are including the key in the statement, so only applicable where you can guarente no collisions. eg. your domain has a 'natural key' or you use a guid. POST is like inserting into a table with an auto incrementing key. You have to be told by the database what ID it got after it has been inserted. Note your "INSERT OR UPDATE" will replace any previous data if it existed.

              – Nigel Thorne
              Nov 26 '18 at 1:33













            • @NigelThorne Thanks for your answer. So if for example I'm trying to PUT a book id 10 with a URI: PUT books/10. If book id 10 does not exists, I should create a book with id 10 right? but I cannot control the creation ID numerator, because it's auto increment. what should I do in that situation ?

              – Roni Axelrad
              Nov 27 '18 at 20:50






            • 1





              @RoniAxelrad REST PUT to an ID that doesn't exist is a request to the server to create a resource. It's still up to the server to decide if it wants to allow that. The server is in charge. It can respond with "No. I'm not going to do that". You already do that if the user doesn't have enough permissions...etc. It's okay for the server to say "No". REST is a convention that lets us define the meaning of various types of request ... your server decides what to do with those requests based on your business logic :) Even if it says "no" it's still following REST :)

              – Nigel Thorne
              Dec 3 '18 at 7:03



















            But how can you use PUT to create a new resource if it doesn't exist, while your ID generation method is on Auto Increment ? Usually ORM's does auto generate the ID for you, like the way you want it to be in a POST for example. Does it mean that if you want to implement PUT the right way you have to change your id auto generation ? This is awkward if the answer is yes.

            – Roni Axelrad
            Sep 16 '18 at 15:28





            But how can you use PUT to create a new resource if it doesn't exist, while your ID generation method is on Auto Increment ? Usually ORM's does auto generate the ID for you, like the way you want it to be in a POST for example. Does it mean that if you want to implement PUT the right way you have to change your id auto generation ? This is awkward if the answer is yes.

            – Roni Axelrad
            Sep 16 '18 at 15:28




            1




            1





            @RoniAxelrad : PUT is like a database "INSERT OR UPDATE" statement where you are including the key in the statement, so only applicable where you can guarente no collisions. eg. your domain has a 'natural key' or you use a guid. POST is like inserting into a table with an auto incrementing key. You have to be told by the database what ID it got after it has been inserted. Note your "INSERT OR UPDATE" will replace any previous data if it existed.

            – Nigel Thorne
            Nov 26 '18 at 1:33







            @RoniAxelrad : PUT is like a database "INSERT OR UPDATE" statement where you are including the key in the statement, so only applicable where you can guarente no collisions. eg. your domain has a 'natural key' or you use a guid. POST is like inserting into a table with an auto incrementing key. You have to be told by the database what ID it got after it has been inserted. Note your "INSERT OR UPDATE" will replace any previous data if it existed.

            – Nigel Thorne
            Nov 26 '18 at 1:33















            @NigelThorne Thanks for your answer. So if for example I'm trying to PUT a book id 10 with a URI: PUT books/10. If book id 10 does not exists, I should create a book with id 10 right? but I cannot control the creation ID numerator, because it's auto increment. what should I do in that situation ?

            – Roni Axelrad
            Nov 27 '18 at 20:50





            @NigelThorne Thanks for your answer. So if for example I'm trying to PUT a book id 10 with a URI: PUT books/10. If book id 10 does not exists, I should create a book with id 10 right? but I cannot control the creation ID numerator, because it's auto increment. what should I do in that situation ?

            – Roni Axelrad
            Nov 27 '18 at 20:50




            1




            1





            @RoniAxelrad REST PUT to an ID that doesn't exist is a request to the server to create a resource. It's still up to the server to decide if it wants to allow that. The server is in charge. It can respond with "No. I'm not going to do that". You already do that if the user doesn't have enough permissions...etc. It's okay for the server to say "No". REST is a convention that lets us define the meaning of various types of request ... your server decides what to do with those requests based on your business logic :) Even if it says "no" it's still following REST :)

            – Nigel Thorne
            Dec 3 '18 at 7:03







            @RoniAxelrad REST PUT to an ID that doesn't exist is a request to the server to create a resource. It's still up to the server to decide if it wants to allow that. The server is in charge. It can respond with "No. I'm not going to do that". You already do that if the user doesn't have enough permissions...etc. It's okay for the server to say "No". REST is a convention that lets us define the meaning of various types of request ... your server decides what to do with those requests based on your business logic :) Even if it says "no" it's still following REST :)

            – Nigel Thorne
            Dec 3 '18 at 7:03













            34














            Ruby on Rails 4.0 will use the 'PATCH' method instead of PUT to do partial updates.



            RFC 5789 says about PATCH (since 1995):




            A new method is necessary to improve interoperability and prevent
            errors. The PUT method is already defined to overwrite a resource
            with a complete new body, and cannot be reused to do partial changes.
            Otherwise, proxies and caches, and even clients and servers, may get
            confused as to the result of the operation. POST is already used but
            without broad interoperability (for one, there is no standard way to
            discover patch format support). PATCH was mentioned in earlier HTTP
            specifications, but not completely defined.




            "Edge Rails: PATCH is the new primary HTTP method for updates" explains it.






            share|improve this answer






























              34














              Ruby on Rails 4.0 will use the 'PATCH' method instead of PUT to do partial updates.



              RFC 5789 says about PATCH (since 1995):




              A new method is necessary to improve interoperability and prevent
              errors. The PUT method is already defined to overwrite a resource
              with a complete new body, and cannot be reused to do partial changes.
              Otherwise, proxies and caches, and even clients and servers, may get
              confused as to the result of the operation. POST is already used but
              without broad interoperability (for one, there is no standard way to
              discover patch format support). PATCH was mentioned in earlier HTTP
              specifications, but not completely defined.




              "Edge Rails: PATCH is the new primary HTTP method for updates" explains it.






              share|improve this answer




























                34












                34








                34







                Ruby on Rails 4.0 will use the 'PATCH' method instead of PUT to do partial updates.



                RFC 5789 says about PATCH (since 1995):




                A new method is necessary to improve interoperability and prevent
                errors. The PUT method is already defined to overwrite a resource
                with a complete new body, and cannot be reused to do partial changes.
                Otherwise, proxies and caches, and even clients and servers, may get
                confused as to the result of the operation. POST is already used but
                without broad interoperability (for one, there is no standard way to
                discover patch format support). PATCH was mentioned in earlier HTTP
                specifications, but not completely defined.




                "Edge Rails: PATCH is the new primary HTTP method for updates" explains it.






                share|improve this answer















                Ruby on Rails 4.0 will use the 'PATCH' method instead of PUT to do partial updates.



                RFC 5789 says about PATCH (since 1995):




                A new method is necessary to improve interoperability and prevent
                errors. The PUT method is already defined to overwrite a resource
                with a complete new body, and cannot be reused to do partial changes.
                Otherwise, proxies and caches, and even clients and servers, may get
                confused as to the result of the operation. POST is already used but
                without broad interoperability (for one, there is no standard way to
                discover patch format support). PATCH was mentioned in earlier HTTP
                specifications, but not completely defined.




                "Edge Rails: PATCH is the new primary HTTP method for updates" explains it.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Sep 16 '17 at 20:17









                Peter Mortensen

                13.7k1986111




                13.7k1986111










                answered Feb 26 '12 at 9:21









                germanlinuxgermanlinux

                2,1351147




                2,1351147























                    33














                    Short Answer:



                    Simple rule of thumb: Use POST to create, use PUT to update.



                    Long Answer:



                    POST:




                    • POST is used to send data to server.

                    • Useful when the resource's URL is
                      unknown


                    PUT:




                    • PUT is used to transfer state to the server

                    • Useful when a resource's URL is known


                    Longer Answer:



                    To understand it we need to question why PUT was required, what were the problems PUT was trying to solve that POST couldn't.



                    From a REST architecture's point of view there is none that matters. We could have lived without PUT as well. But from a client developer's point of view it made his/her life a lot simpler.



                    Prior to PUT, clients couldn't directly know the URL that the server generated or if all it had generated any or whether the data to be sent to the server is already updated or not. PUT relieved the developer of all these headaches. PUT is idempotent, PUT handles race conditions, and PUT lets the client choose the URL.






                    share|improve this answer





















                    • 3





                      Your short answer might be VERY wrong. HTTP PUT is free to be repeated by HTTP proxies. And so, if PUT is actually doing SQL INSERT it might fail second time, which means it would return different result and so it would not be IDEMPOTENT (which is the difference between PUT and POST)

                      – Kamil Tomšík
                      Apr 30 '18 at 17:27
















                    33














                    Short Answer:



                    Simple rule of thumb: Use POST to create, use PUT to update.



                    Long Answer:



                    POST:




                    • POST is used to send data to server.

                    • Useful when the resource's URL is
                      unknown


                    PUT:




                    • PUT is used to transfer state to the server

                    • Useful when a resource's URL is known


                    Longer Answer:



                    To understand it we need to question why PUT was required, what were the problems PUT was trying to solve that POST couldn't.



                    From a REST architecture's point of view there is none that matters. We could have lived without PUT as well. But from a client developer's point of view it made his/her life a lot simpler.



                    Prior to PUT, clients couldn't directly know the URL that the server generated or if all it had generated any or whether the data to be sent to the server is already updated or not. PUT relieved the developer of all these headaches. PUT is idempotent, PUT handles race conditions, and PUT lets the client choose the URL.






                    share|improve this answer





















                    • 3





                      Your short answer might be VERY wrong. HTTP PUT is free to be repeated by HTTP proxies. And so, if PUT is actually doing SQL INSERT it might fail second time, which means it would return different result and so it would not be IDEMPOTENT (which is the difference between PUT and POST)

                      – Kamil Tomšík
                      Apr 30 '18 at 17:27














                    33












                    33








                    33







                    Short Answer:



                    Simple rule of thumb: Use POST to create, use PUT to update.



                    Long Answer:



                    POST:




                    • POST is used to send data to server.

                    • Useful when the resource's URL is
                      unknown


                    PUT:




                    • PUT is used to transfer state to the server

                    • Useful when a resource's URL is known


                    Longer Answer:



                    To understand it we need to question why PUT was required, what were the problems PUT was trying to solve that POST couldn't.



                    From a REST architecture's point of view there is none that matters. We could have lived without PUT as well. But from a client developer's point of view it made his/her life a lot simpler.



                    Prior to PUT, clients couldn't directly know the URL that the server generated or if all it had generated any or whether the data to be sent to the server is already updated or not. PUT relieved the developer of all these headaches. PUT is idempotent, PUT handles race conditions, and PUT lets the client choose the URL.






                    share|improve this answer















                    Short Answer:



                    Simple rule of thumb: Use POST to create, use PUT to update.



                    Long Answer:



                    POST:




                    • POST is used to send data to server.

                    • Useful when the resource's URL is
                      unknown


                    PUT:




                    • PUT is used to transfer state to the server

                    • Useful when a resource's URL is known


                    Longer Answer:



                    To understand it we need to question why PUT was required, what were the problems PUT was trying to solve that POST couldn't.



                    From a REST architecture's point of view there is none that matters. We could have lived without PUT as well. But from a client developer's point of view it made his/her life a lot simpler.



                    Prior to PUT, clients couldn't directly know the URL that the server generated or if all it had generated any or whether the data to be sent to the server is already updated or not. PUT relieved the developer of all these headaches. PUT is idempotent, PUT handles race conditions, and PUT lets the client choose the URL.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Oct 21 '17 at 13:03









                    Peter Mortensen

                    13.7k1986111




                    13.7k1986111










                    answered Oct 15 '17 at 2:33









                    ishandutta2007ishandutta2007

                    4,71364258




                    4,71364258








                    • 3





                      Your short answer might be VERY wrong. HTTP PUT is free to be repeated by HTTP proxies. And so, if PUT is actually doing SQL INSERT it might fail second time, which means it would return different result and so it would not be IDEMPOTENT (which is the difference between PUT and POST)

                      – Kamil Tomšík
                      Apr 30 '18 at 17:27














                    • 3





                      Your short answer might be VERY wrong. HTTP PUT is free to be repeated by HTTP proxies. And so, if PUT is actually doing SQL INSERT it might fail second time, which means it would return different result and so it would not be IDEMPOTENT (which is the difference between PUT and POST)

                      – Kamil Tomšík
                      Apr 30 '18 at 17:27








                    3




                    3





                    Your short answer might be VERY wrong. HTTP PUT is free to be repeated by HTTP proxies. And so, if PUT is actually doing SQL INSERT it might fail second time, which means it would return different result and so it would not be IDEMPOTENT (which is the difference between PUT and POST)

                    – Kamil Tomšík
                    Apr 30 '18 at 17:27





                    Your short answer might be VERY wrong. HTTP PUT is free to be repeated by HTTP proxies. And so, if PUT is actually doing SQL INSERT it might fail second time, which means it would return different result and so it would not be IDEMPOTENT (which is the difference between PUT and POST)

                    – Kamil Tomšík
                    Apr 30 '18 at 17:27











                    26














                    At the risk of restating what has already been said, it seems important to remember that PUT implies that the client controls what the URL is going to end up being, when creating a resource. So part of the choice between PUT and POST is going to be about how much you can trust the client to provide correct, normalized URL that are coherent with whatever your URL scheme is.



                    When you can't fully trust the client to do the right thing, it would be
                    more appropriate to use POST to create a new item and then send the URL back to the client in the response.






                    share|improve this answer





















                    • 2





                      I'm a bit late to this - but someone saying something similar on another website got it to click for me. If you're creating a resource and using an auto-incremented ID as it's "identifier" instead of a user assigned name, it should be a POST.

                      – Ixmatus
                      Feb 3 '12 at 18:51






                    • 2





                      This isn't quite right - PUT can still create a resource by referring to it with a non-canonical name, as long as in the response, the server returns a Location header that does contain the canonical resource name.

                      – Ether
                      Oct 19 '12 at 16:08






                    • 1





                      @Joshcodes don't forget that you can have many URIs referencing the same underlying resource. So what Ether said is sound advice, the client can PUT to a URL (that might be more semantic, like PUT /X-files/series/4/episodes/max) and the server respond with a URI that provides a short canonical unique link to that new resource (ie /X-Ffiles/episodes/91)

                      – thecoshman
                      Jun 8 '15 at 8:02











                    • @thecoshman the issue is the concern for the URL structure does not belong to the client. Reading about self-discovery (also part of REST) may help make this clear.

                      – Joshcodes
                      Jun 8 '15 at 17:50











                    • @Joshcodes then by that logic, a client should never use PUT to create as they shouldn't be concerned with with providing the URL. Well... unless the server provided a URL to PUT to if the client wants to put to it... something like "PUT /comments/new" and the server might respond "204 /comments/234532" but that seems a bit RPC to me, the client should just POST to /comments...

                      – thecoshman
                      Jun 9 '15 at 16:18
















                    26














                    At the risk of restating what has already been said, it seems important to remember that PUT implies that the client controls what the URL is going to end up being, when creating a resource. So part of the choice between PUT and POST is going to be about how much you can trust the client to provide correct, normalized URL that are coherent with whatever your URL scheme is.



                    When you can't fully trust the client to do the right thing, it would be
                    more appropriate to use POST to create a new item and then send the URL back to the client in the response.






                    share|improve this answer





















                    • 2





                      I'm a bit late to this - but someone saying something similar on another website got it to click for me. If you're creating a resource and using an auto-incremented ID as it's "identifier" instead of a user assigned name, it should be a POST.

                      – Ixmatus
                      Feb 3 '12 at 18:51






                    • 2





                      This isn't quite right - PUT can still create a resource by referring to it with a non-canonical name, as long as in the response, the server returns a Location header that does contain the canonical resource name.

                      – Ether
                      Oct 19 '12 at 16:08






                    • 1





                      @Joshcodes don't forget that you can have many URIs referencing the same underlying resource. So what Ether said is sound advice, the client can PUT to a URL (that might be more semantic, like PUT /X-files/series/4/episodes/max) and the server respond with a URI that provides a short canonical unique link to that new resource (ie /X-Ffiles/episodes/91)

                      – thecoshman
                      Jun 8 '15 at 8:02











                    • @thecoshman the issue is the concern for the URL structure does not belong to the client. Reading about self-discovery (also part of REST) may help make this clear.

                      – Joshcodes
                      Jun 8 '15 at 17:50











                    • @Joshcodes then by that logic, a client should never use PUT to create as they shouldn't be concerned with with providing the URL. Well... unless the server provided a URL to PUT to if the client wants to put to it... something like "PUT /comments/new" and the server might respond "204 /comments/234532" but that seems a bit RPC to me, the client should just POST to /comments...

                      – thecoshman
                      Jun 9 '15 at 16:18














                    26












                    26








                    26







                    At the risk of restating what has already been said, it seems important to remember that PUT implies that the client controls what the URL is going to end up being, when creating a resource. So part of the choice between PUT and POST is going to be about how much you can trust the client to provide correct, normalized URL that are coherent with whatever your URL scheme is.



                    When you can't fully trust the client to do the right thing, it would be
                    more appropriate to use POST to create a new item and then send the URL back to the client in the response.






                    share|improve this answer















                    At the risk of restating what has already been said, it seems important to remember that PUT implies that the client controls what the URL is going to end up being, when creating a resource. So part of the choice between PUT and POST is going to be about how much you can trust the client to provide correct, normalized URL that are coherent with whatever your URL scheme is.



                    When you can't fully trust the client to do the right thing, it would be
                    more appropriate to use POST to create a new item and then send the URL back to the client in the response.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Aug 28 '14 at 9:17









                    beginer

                    1587




                    1587










                    answered Mar 25 '11 at 10:17









                    skillet-thiefskillet-thief

                    45948




                    45948








                    • 2





                      I'm a bit late to this - but someone saying something similar on another website got it to click for me. If you're creating a resource and using an auto-incremented ID as it's "identifier" instead of a user assigned name, it should be a POST.

                      – Ixmatus
                      Feb 3 '12 at 18:51






                    • 2





                      This isn't quite right - PUT can still create a resource by referring to it with a non-canonical name, as long as in the response, the server returns a Location header that does contain the canonical resource name.

                      – Ether
                      Oct 19 '12 at 16:08






                    • 1





                      @Joshcodes don't forget that you can have many URIs referencing the same underlying resource. So what Ether said is sound advice, the client can PUT to a URL (that might be more semantic, like PUT /X-files/series/4/episodes/max) and the server respond with a URI that provides a short canonical unique link to that new resource (ie /X-Ffiles/episodes/91)

                      – thecoshman
                      Jun 8 '15 at 8:02











                    • @thecoshman the issue is the concern for the URL structure does not belong to the client. Reading about self-discovery (also part of REST) may help make this clear.

                      – Joshcodes
                      Jun 8 '15 at 17:50











                    • @Joshcodes then by that logic, a client should never use PUT to create as they shouldn't be concerned with with providing the URL. Well... unless the server provided a URL to PUT to if the client wants to put to it... something like "PUT /comments/new" and the server might respond "204 /comments/234532" but that seems a bit RPC to me, the client should just POST to /comments...

                      – thecoshman
                      Jun 9 '15 at 16:18














                    • 2





                      I'm a bit late to this - but someone saying something similar on another website got it to click for me. If you're creating a resource and using an auto-incremented ID as it's "identifier" instead of a user assigned name, it should be a POST.

                      – Ixmatus
                      Feb 3 '12 at 18:51






                    • 2





                      This isn't quite right - PUT can still create a resource by referring to it with a non-canonical name, as long as in the response, the server returns a Location header that does contain the canonical resource name.

                      – Ether
                      Oct 19 '12 at 16:08






                    • 1





                      @Joshcodes don't forget that you can have many URIs referencing the same underlying resource. So what Ether said is sound advice, the client can PUT to a URL (that might be more semantic, like PUT /X-files/series/4/episodes/max) and the server respond with a URI that provides a short canonical unique link to that new resource (ie /X-Ffiles/episodes/91)

                      – thecoshman
                      Jun 8 '15 at 8:02











                    • @thecoshman the issue is the concern for the URL structure does not belong to the client. Reading about self-discovery (also part of REST) may help make this clear.

                      – Joshcodes
                      Jun 8 '15 at 17:50











                    • @Joshcodes then by that logic, a client should never use PUT to create as they shouldn't be concerned with with providing the URL. Well... unless the server provided a URL to PUT to if the client wants to put to it... something like "PUT /comments/new" and the server might respond "204 /comments/234532" but that seems a bit RPC to me, the client should just POST to /comments...

                      – thecoshman
                      Jun 9 '15 at 16:18








                    2




                    2





                    I'm a bit late to this - but someone saying something similar on another website got it to click for me. If you're creating a resource and using an auto-incremented ID as it's "identifier" instead of a user assigned name, it should be a POST.

                    – Ixmatus
                    Feb 3 '12 at 18:51





                    I'm a bit late to this - but someone saying something similar on another website got it to click for me. If you're creating a resource and using an auto-incremented ID as it's "identifier" instead of a user assigned name, it should be a POST.

                    – Ixmatus
                    Feb 3 '12 at 18:51




                    2




                    2





                    This isn't quite right - PUT can still create a resource by referring to it with a non-canonical name, as long as in the response, the server returns a Location header that does contain the canonical resource name.

                    – Ether
                    Oct 19 '12 at 16:08





                    This isn't quite right - PUT can still create a resource by referring to it with a non-canonical name, as long as in the response, the server returns a Location header that does contain the canonical resource name.

                    – Ether
                    Oct 19 '12 at 16:08




                    1




                    1





                    @Joshcodes don't forget that you can have many URIs referencing the same underlying resource. So what Ether said is sound advice, the client can PUT to a URL (that might be more semantic, like PUT /X-files/series/4/episodes/max) and the server respond with a URI that provides a short canonical unique link to that new resource (ie /X-Ffiles/episodes/91)

                    – thecoshman
                    Jun 8 '15 at 8:02





                    @Joshcodes don't forget that you can have many URIs referencing the same underlying resource. So what Ether said is sound advice, the client can PUT to a URL (that might be more semantic, like PUT /X-files/series/4/episodes/max) and the server respond with a URI that provides a short canonical unique link to that new resource (ie /X-Ffiles/episodes/91)

                    – thecoshman
                    Jun 8 '15 at 8:02













                    @thecoshman the issue is the concern for the URL structure does not belong to the client. Reading about self-discovery (also part of REST) may help make this clear.

                    – Joshcodes
                    Jun 8 '15 at 17:50





                    @thecoshman the issue is the concern for the URL structure does not belong to the client. Reading about self-discovery (also part of REST) may help make this clear.

                    – Joshcodes
                    Jun 8 '15 at 17:50













                    @Joshcodes then by that logic, a client should never use PUT to create as they shouldn't be concerned with with providing the URL. Well... unless the server provided a URL to PUT to if the client wants to put to it... something like "PUT /comments/new" and the server might respond "204 /comments/234532" but that seems a bit RPC to me, the client should just POST to /comments...

                    – thecoshman
                    Jun 9 '15 at 16:18





                    @Joshcodes then by that logic, a client should never use PUT to create as they shouldn't be concerned with with providing the URL. Well... unless the server provided a URL to PUT to if the client wants to put to it... something like "PUT /comments/new" and the server might respond "204 /comments/234532" but that seems a bit RPC to me, the client should just POST to /comments...

                    – thecoshman
                    Jun 9 '15 at 16:18











                    20














                    The most important consideration is reliability. If a POST message gets lost the state of the system is undefined. Automatic recovery is impossible. For PUT messages, the state is undefined only until the first successful retry.



                    For instance, it may not be a good idea to create credit card transactions with POST.



                    If you happen to have auto generated URI's on your resource you can still use PUT by passing a generated URI (pointing to an empty resource) to the client.



                    Some other considerations:




                    • POST invalidates cached copies of the entire containing resource (better consistency)

                    • PUT responses are not cacheable while POST ones are (Require Content-Location and expiration)

                    • PUT is less supported by e.g. Java ME, older browsers, firewalls






                    share|improve this answer


























                    • This is incorrect. For POST, the state is also undefined only until the first successful retry. Then, either the server accepts the POST (message never arrived), throws a 409 conflict for a duplicate ID (message arrived, response was lost), or any other valid response.

                      – Joshcodes
                      Apr 24 '14 at 12:13











                    • In general a useragent would not able to safely retry the POST operation since the POST operation gives no that guarantee that two operations would have the same effect as one. The term "ID" has nothing to do with HTTP. The URI identifies the resource.

                      – Hans Malherbe
                      Jul 25 '14 at 5:48











                    • A useragent can "safely" retry a POST operation as many times as it wants. It will just receive a duplicate ID error (assuming the resource has an ID) or a duplicate data error (assuming that's an issue and the resource does not have IDs).

                      – Joshcodes
                      Jul 27 '14 at 2:10











                    • Bangs head against wall. HTTP has no solution to the problem of reliability, and this is not well understood, not much discussed, and simply not catered for in the vast majority of web applications. @Joshcodes I have an answer to this question. I essentially agree with Hans. There's a problem.

                      – bbsimonbb
                      Jun 13 '18 at 8:21











                    • @bbsimonbb, HTTP has a robust and well documented set of error responses. My answer to this question (stackoverflow.com/questions/630453/put-vs-post-in-rest/…) covers how to use http according to specification to achieve consistency.

                      – Joshcodes
                      Jun 19 '18 at 12:50
















                    20














                    The most important consideration is reliability. If a POST message gets lost the state of the system is undefined. Automatic recovery is impossible. For PUT messages, the state is undefined only until the first successful retry.



                    For instance, it may not be a good idea to create credit card transactions with POST.



                    If you happen to have auto generated URI's on your resource you can still use PUT by passing a generated URI (pointing to an empty resource) to the client.



                    Some other considerations:




                    • POST invalidates cached copies of the entire containing resource (better consistency)

                    • PUT responses are not cacheable while POST ones are (Require Content-Location and expiration)

                    • PUT is less supported by e.g. Java ME, older browsers, firewalls






                    share|improve this answer


























                    • This is incorrect. For POST, the state is also undefined only until the first successful retry. Then, either the server accepts the POST (message never arrived), throws a 409 conflict for a duplicate ID (message arrived, response was lost), or any other valid response.

                      – Joshcodes
                      Apr 24 '14 at 12:13











                    • In general a useragent would not able to safely retry the POST operation since the POST operation gives no that guarantee that two operations would have the same effect as one. The term "ID" has nothing to do with HTTP. The URI identifies the resource.

                      – Hans Malherbe
                      Jul 25 '14 at 5:48











                    • A useragent can "safely" retry a POST operation as many times as it wants. It will just receive a duplicate ID error (assuming the resource has an ID) or a duplicate data error (assuming that's an issue and the resource does not have IDs).

                      – Joshcodes
                      Jul 27 '14 at 2:10











                    • Bangs head against wall. HTTP has no solution to the problem of reliability, and this is not well understood, not much discussed, and simply not catered for in the vast majority of web applications. @Joshcodes I have an answer to this question. I essentially agree with Hans. There's a problem.

                      – bbsimonbb
                      Jun 13 '18 at 8:21











                    • @bbsimonbb, HTTP has a robust and well documented set of error responses. My answer to this question (stackoverflow.com/questions/630453/put-vs-post-in-rest/…) covers how to use http according to specification to achieve consistency.

                      – Joshcodes
                      Jun 19 '18 at 12:50














                    20












                    20








                    20







                    The most important consideration is reliability. If a POST message gets lost the state of the system is undefined. Automatic recovery is impossible. For PUT messages, the state is undefined only until the first successful retry.



                    For instance, it may not be a good idea to create credit card transactions with POST.



                    If you happen to have auto generated URI's on your resource you can still use PUT by passing a generated URI (pointing to an empty resource) to the client.



                    Some other considerations:




                    • POST invalidates cached copies of the entire containing resource (better consistency)

                    • PUT responses are not cacheable while POST ones are (Require Content-Location and expiration)

                    • PUT is less supported by e.g. Java ME, older browsers, firewalls






                    share|improve this answer















                    The most important consideration is reliability. If a POST message gets lost the state of the system is undefined. Automatic recovery is impossible. For PUT messages, the state is undefined only until the first successful retry.



                    For instance, it may not be a good idea to create credit card transactions with POST.



                    If you happen to have auto generated URI's on your resource you can still use PUT by passing a generated URI (pointing to an empty resource) to the client.



                    Some other considerations:




                    • POST invalidates cached copies of the entire containing resource (better consistency)

                    • PUT responses are not cacheable while POST ones are (Require Content-Location and expiration)

                    • PUT is less supported by e.g. Java ME, older browsers, firewalls







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Feb 10 '12 at 5:53

























                    answered Feb 8 '12 at 16:31









                    Hans MalherbeHans Malherbe

                    2,4401518




                    2,4401518













                    • This is incorrect. For POST, the state is also undefined only until the first successful retry. Then, either the server accepts the POST (message never arrived), throws a 409 conflict for a duplicate ID (message arrived, response was lost), or any other valid response.

                      – Joshcodes
                      Apr 24 '14 at 12:13











                    • In general a useragent would not able to safely retry the POST operation since the POST operation gives no that guarantee that two operations would have the same effect as one. The term "ID" has nothing to do with HTTP. The URI identifies the resource.

                      – Hans Malherbe
                      Jul 25 '14 at 5:48











                    • A useragent can "safely" retry a POST operation as many times as it wants. It will just receive a duplicate ID error (assuming the resource has an ID) or a duplicate data error (assuming that's an issue and the resource does not have IDs).

                      – Joshcodes
                      Jul 27 '14 at 2:10











                    • Bangs head against wall. HTTP has no solution to the problem of reliability, and this is not well understood, not much discussed, and simply not catered for in the vast majority of web applications. @Joshcodes I have an answer to this question. I essentially agree with Hans. There's a problem.

                      – bbsimonbb
                      Jun 13 '18 at 8:21











                    • @bbsimonbb, HTTP has a robust and well documented set of error responses. My answer to this question (stackoverflow.com/questions/630453/put-vs-post-in-rest/…) covers how to use http according to specification to achieve consistency.

                      – Joshcodes
                      Jun 19 '18 at 12:50



















                    • This is incorrect. For POST, the state is also undefined only until the first successful retry. Then, either the server accepts the POST (message never arrived), throws a 409 conflict for a duplicate ID (message arrived, response was lost), or any other valid response.

                      – Joshcodes
                      Apr 24 '14 at 12:13











                    • In general a useragent would not able to safely retry the POST operation since the POST operation gives no that guarantee that two operations would have the same effect as one. The term "ID" has nothing to do with HTTP. The URI identifies the resource.

                      – Hans Malherbe
                      Jul 25 '14 at 5:48











                    • A useragent can "safely" retry a POST operation as many times as it wants. It will just receive a duplicate ID error (assuming the resource has an ID) or a duplicate data error (assuming that's an issue and the resource does not have IDs).

                      – Joshcodes
                      Jul 27 '14 at 2:10











                    • Bangs head against wall. HTTP has no solution to the problem of reliability, and this is not well understood, not much discussed, and simply not catered for in the vast majority of web applications. @Joshcodes I have an answer to this question. I essentially agree with Hans. There's a problem.

                      – bbsimonbb
                      Jun 13 '18 at 8:21











                    • @bbsimonbb, HTTP has a robust and well documented set of error responses. My answer to this question (stackoverflow.com/questions/630453/put-vs-post-in-rest/…) covers how to use http according to specification to achieve consistency.

                      – Joshcodes
                      Jun 19 '18 at 12:50

















                    This is incorrect. For POST, the state is also undefined only until the first successful retry. Then, either the server accepts the POST (message never arrived), throws a 409 conflict for a duplicate ID (message arrived, response was lost), or any other valid response.

                    – Joshcodes
                    Apr 24 '14 at 12:13





                    This is incorrect. For POST, the state is also undefined only until the first successful retry. Then, either the server accepts the POST (message never arrived), throws a 409 conflict for a duplicate ID (message arrived, response was lost), or any other valid response.

                    – Joshcodes
                    Apr 24 '14 at 12:13













                    In general a useragent would not able to safely retry the POST operation since the POST operation gives no that guarantee that two operations would have the same effect as one. The term "ID" has nothing to do with HTTP. The URI identifies the resource.

                    – Hans Malherbe
                    Jul 25 '14 at 5:48





                    In general a useragent would not able to safely retry the POST operation since the POST operation gives no that guarantee that two operations would have the same effect as one. The term "ID" has nothing to do with HTTP. The URI identifies the resource.

                    – Hans Malherbe
                    Jul 25 '14 at 5:48













                    A useragent can "safely" retry a POST operation as many times as it wants. It will just receive a duplicate ID error (assuming the resource has an ID) or a duplicate data error (assuming that's an issue and the resource does not have IDs).

                    – Joshcodes
                    Jul 27 '14 at 2:10





                    A useragent can "safely" retry a POST operation as many times as it wants. It will just receive a duplicate ID error (assuming the resource has an ID) or a duplicate data error (assuming that's an issue and the resource does not have IDs).

                    – Joshcodes
                    Jul 27 '14 at 2:10













                    Bangs head against wall. HTTP has no solution to the problem of reliability, and this is not well understood, not much discussed, and simply not catered for in the vast majority of web applications. @Joshcodes I have an answer to this question. I essentially agree with Hans. There's a problem.

                    – bbsimonbb
                    Jun 13 '18 at 8:21





                    Bangs head against wall. HTTP has no solution to the problem of reliability, and this is not well understood, not much discussed, and simply not catered for in the vast majority of web applications. @Joshcodes I have an answer to this question. I essentially agree with Hans. There's a problem.

                    – bbsimonbb
                    Jun 13 '18 at 8:21













                    @bbsimonbb, HTTP has a robust and well documented set of error responses. My answer to this question (stackoverflow.com/questions/630453/put-vs-post-in-rest/…) covers how to use http according to specification to achieve consistency.

                    – Joshcodes
                    Jun 19 '18 at 12:50





                    @bbsimonbb, HTTP has a robust and well documented set of error responses. My answer to this question (stackoverflow.com/questions/630453/put-vs-post-in-rest/…) covers how to use http according to specification to achieve consistency.

                    – Joshcodes
                    Jun 19 '18 at 12:50











                    20














                    In a very simple way I'm taking the example of the Facebook timeline.



                    Case 1: When you post something on your timeline, it's a fresh new entry. So in this case they use the POST method because the POST method is non-idempotent.



                    Case 2: If your friend comment on your post the first time, that also will create a new entry in the database so the POST method used.



                    Case 3: If your friend edits his comment, in this case, they had a comment id, so they will update an existing comment instead of creating a new entry in the database. Therefore for this type of operation use the PUT method because it is idempotent.*



                    In a single line, use POST to add a new entry in the database and PUT to update something in the database.






                    share|improve this answer





















                    • 4





                      If the comment is an object with property like user id, created date, comment-message, etc. and at the time of edit only comment-message is getting updated, PATCH should be done here?

                      – Habeeb Perwad
                      Aug 12 '17 at 10:47











                    • PUT is used by FB to update the comment because an existing resource is being updated, and that is what PUT does (updates a resource). PUT happens to be idempotent, in contrast to POST. An HTTP verb being idempotent affects the error handling but does not dictate usage. See my answer for a more detail explanation: stackoverflow.com/questions/630453/put-vs-post-in-rest/…

                      – Joshcodes
                      Jun 19 '18 at 12:57
















                    20














                    In a very simple way I'm taking the example of the Facebook timeline.



                    Case 1: When you post something on your timeline, it's a fresh new entry. So in this case they use the POST method because the POST method is non-idempotent.



                    Case 2: If your friend comment on your post the first time, that also will create a new entry in the database so the POST method used.



                    Case 3: If your friend edits his comment, in this case, they had a comment id, so they will update an existing comment instead of creating a new entry in the database. Therefore for this type of operation use the PUT method because it is idempotent.*



                    In a single line, use POST to add a new entry in the database and PUT to update something in the database.






                    share|improve this answer





















                    • 4





                      If the comment is an object with property like user id, created date, comment-message, etc. and at the time of edit only comment-message is getting updated, PATCH should be done here?

                      – Habeeb Perwad
                      Aug 12 '17 at 10:47











                    • PUT is used by FB to update the comment because an existing resource is being updated, and that is what PUT does (updates a resource). PUT happens to be idempotent, in contrast to POST. An HTTP verb being idempotent affects the error handling but does not dictate usage. See my answer for a more detail explanation: stackoverflow.com/questions/630453/put-vs-post-in-rest/…

                      – Joshcodes
                      Jun 19 '18 at 12:57














                    20












                    20








                    20







                    In a very simple way I'm taking the example of the Facebook timeline.



                    Case 1: When you post something on your timeline, it's a fresh new entry. So in this case they use the POST method because the POST method is non-idempotent.



                    Case 2: If your friend comment on your post the first time, that also will create a new entry in the database so the POST method used.



                    Case 3: If your friend edits his comment, in this case, they had a comment id, so they will update an existing comment instead of creating a new entry in the database. Therefore for this type of operation use the PUT method because it is idempotent.*



                    In a single line, use POST to add a new entry in the database and PUT to update something in the database.






                    share|improve this answer















                    In a very simple way I'm taking the example of the Facebook timeline.



                    Case 1: When you post something on your timeline, it's a fresh new entry. So in this case they use the POST method because the POST method is non-idempotent.



                    Case 2: If your friend comment on your post the first time, that also will create a new entry in the database so the POST method used.



                    Case 3: If your friend edits his comment, in this case, they had a comment id, so they will update an existing comment instead of creating a new entry in the database. Therefore for this type of operation use the PUT method because it is idempotent.*



                    In a single line, use POST to add a new entry in the database and PUT to update something in the database.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Oct 21 '17 at 12:49









                    Peter Mortensen

                    13.7k1986111




                    13.7k1986111










                    answered Feb 14 '17 at 6:55









                    UniCoderUniCoder

                    1,1441316




                    1,1441316








                    • 4





                      If the comment is an object with property like user id, created date, comment-message, etc. and at the time of edit only comment-message is getting updated, PATCH should be done here?

                      – Habeeb Perwad
                      Aug 12 '17 at 10:47











                    • PUT is used by FB to update the comment because an existing resource is being updated, and that is what PUT does (updates a resource). PUT happens to be idempotent, in contrast to POST. An HTTP verb being idempotent affects the error handling but does not dictate usage. See my answer for a more detail explanation: stackoverflow.com/questions/630453/put-vs-post-in-rest/…

                      – Joshcodes
                      Jun 19 '18 at 12:57














                    • 4





                      If the comment is an object with property like user id, created date, comment-message, etc. and at the time of edit only comment-message is getting updated, PATCH should be done here?

                      – Habeeb Perwad
                      Aug 12 '17 at 10:47











                    • PUT is used by FB to update the comment because an existing resource is being updated, and that is what PUT does (updates a resource). PUT happens to be idempotent, in contrast to POST. An HTTP verb being idempotent affects the error handling but does not dictate usage. See my answer for a more detail explanation: stackoverflow.com/questions/630453/put-vs-post-in-rest/…

                      – Joshcodes
                      Jun 19 '18 at 12:57








                    4




                    4





                    If the comment is an object with property like user id, created date, comment-message, etc. and at the time of edit only comment-message is getting updated, PATCH should be done here?

                    – Habeeb Perwad
                    Aug 12 '17 at 10:47





                    If the comment is an object with property like user id, created date, comment-message, etc. and at the time of edit only comment-message is getting updated, PATCH should be done here?

                    – Habeeb Perwad
                    Aug 12 '17 at 10:47













                    PUT is used by FB to update the comment because an existing resource is being updated, and that is what PUT does (updates a resource). PUT happens to be idempotent, in contrast to POST. An HTTP verb being idempotent affects the error handling but does not dictate usage. See my answer for a more detail explanation: stackoverflow.com/questions/630453/put-vs-post-in-rest/…

                    – Joshcodes
                    Jun 19 '18 at 12:57





                    PUT is used by FB to update the comment because an existing resource is being updated, and that is what PUT does (updates a resource). PUT happens to be idempotent, in contrast to POST. An HTTP verb being idempotent affects the error handling but does not dictate usage. See my answer for a more detail explanation: stackoverflow.com/questions/630453/put-vs-post-in-rest/…

                    – Joshcodes
                    Jun 19 '18 at 12:57











                    14














                    There seems to always be some confusion as to when to use the HTTP POST versus the HTTP PUT method for REST services. Most developers will try to associate CRUD operations directly to HTTP methods. I will argue that this is not correct and one can not simply associate the CRUD concepts to the HTTP methods. That is:



                    Create => HTTP PUT
                    Retrieve => HTTP GET
                    Update => HTTP POST
                    Delete => HTTP DELETE


                    It is true that the R(etrieve) and D(elete) of the CRUD operations can be mapped directly to the HTTP methods GET and DELETE respectively. However, the confusion lies in the C(reate) and U(update) operations. In some cases, one can use the PUT for a create while in other cases a POST will be required. The ambiguity lies in the definition of an HTTP PUT method versus an HTTP POST method.



                    According to the HTTP 1.1 specifications the GET, HEAD, DELETE, and PUT methods must be idempotent, and the POST method is not idempotent. That is to say that an operation is idempotent if it can be performed on a resource once or many times and always return the same state of that resource. Whereas a non idempotent operation can return a modified state of the resource from one request to another. Hence, in a non idempotent operation, there is no guarantee that one will receive the same state of a resource.



                    Based on the above idempotent definition, my take on using the HTTP PUT method versus using the HTTP POST method for REST services is:
                    Use the HTTP PUT method when:



                    The client includes all aspect of the resource including the unique identifier to uniquely identify the resource. Example: creating a new employee.
                    The client provides all the information for a resource to be able to modify that resource.This implies that the server side does not update any aspect of the resource (such as an update date).


                    In both cases, these operations can be performed multiple times with the same results. That is the resource will not be changed by requesting the operation more than once. Hence, a true idempotent operation.
                    Use the HTTP POST method when:



                    The server will provide some information concerning the newly created resource. For example, take a logging system. A new entry in the log will most likely have a numbering scheme which is determined on the server side. Upon creating a new log entry, the new sequence number will be determined by the server and not by the client.
                    On a modification of a resource, the server will provide such information as a resource state or an update date. Again in this case not all information was provided by the client and the resource will be changing from one modification request to the next. Hence a non idempotent operation.


                    Conclusion



                    Do not directly correlate and map CRUD operations to HTTP methods for REST services. The use of an HTTP PUT method versus an HTTP POST method should be based on the idempotent aspect of that operation. That is, if the operation is idempotent, then use the HTTP PUT method. If the operation is non idempotent, then use the HTTP POST method.






                    share|improve this answer



















                    • 2





                      Update => HTTP POST : POST is not for updating

                      – Premraj
                      Jan 30 '16 at 0:57


















                    14














                    There seems to always be some confusion as to when to use the HTTP POST versus the HTTP PUT method for REST services. Most developers will try to associate CRUD operations directly to HTTP methods. I will argue that this is not correct and one can not simply associate the CRUD concepts to the HTTP methods. That is:



                    Create => HTTP PUT
                    Retrieve => HTTP GET
                    Update => HTTP POST
                    Delete => HTTP DELETE


                    It is true that the R(etrieve) and D(elete) of the CRUD operations can be mapped directly to the HTTP methods GET and DELETE respectively. However, the confusion lies in the C(reate) and U(update) operations. In some cases, one can use the PUT for a create while in other cases a POST will be required. The ambiguity lies in the definition of an HTTP PUT method versus an HTTP POST method.



                    According to the HTTP 1.1 specifications the GET, HEAD, DELETE, and PUT methods must be idempotent, and the POST method is not idempotent. That is to say that an operation is idempotent if it can be performed on a resource once or many times and always return the same state of that resource. Whereas a non idempotent operation can return a modified state of the resource from one request to another. Hence, in a non idempotent operation, there is no guarantee that one will receive the same state of a resource.



                    Based on the above idempotent definition, my take on using the HTTP PUT method versus using the HTTP POST method for REST services is:
                    Use the HTTP PUT method when:



                    The client includes all aspect of the resource including the unique identifier to uniquely identify the resource. Example: creating a new employee.
                    The client provides all the information for a resource to be able to modify that resource.This implies that the server side does not update any aspect of the resource (such as an update date).


                    In both cases, these operations can be performed multiple times with the same results. That is the resource will not be changed by requesting the operation more than once. Hence, a true idempotent operation.
                    Use the HTTP POST method when:



                    The server will provide some information concerning the newly created resource. For example, take a logging system. A new entry in the log will most likely have a numbering scheme which is determined on the server side. Upon creating a new log entry, the new sequence number will be determined by the server and not by the client.
                    On a modification of a resource, the server will provide such information as a resource state or an update date. Again in this case not all information was provided by the client and the resource will be changing from one modification request to the next. Hence a non idempotent operation.


                    Conclusion



                    Do not directly correlate and map CRUD operations to HTTP methods for REST services. The use of an HTTP PUT method versus an HTTP POST method should be based on the idempotent aspect of that operation. That is, if the operation is idempotent, then use the HTTP PUT method. If the operation is non idempotent, then use the HTTP POST method.






                    share|improve this answer



















                    • 2





                      Update => HTTP POST : POST is not for updating

                      – Premraj
                      Jan 30 '16 at 0:57
















                    14












                    14








                    14







                    There seems to always be some confusion as to when to use the HTTP POST versus the HTTP PUT method for REST services. Most developers will try to associate CRUD operations directly to HTTP methods. I will argue that this is not correct and one can not simply associate the CRUD concepts to the HTTP methods. That is:



                    Create => HTTP PUT
                    Retrieve => HTTP GET
                    Update => HTTP POST
                    Delete => HTTP DELETE


                    It is true that the R(etrieve) and D(elete) of the CRUD operations can be mapped directly to the HTTP methods GET and DELETE respectively. However, the confusion lies in the C(reate) and U(update) operations. In some cases, one can use the PUT for a create while in other cases a POST will be required. The ambiguity lies in the definition of an HTTP PUT method versus an HTTP POST method.



                    According to the HTTP 1.1 specifications the GET, HEAD, DELETE, and PUT methods must be idempotent, and the POST method is not idempotent. That is to say that an operation is idempotent if it can be performed on a resource once or many times and always return the same state of that resource. Whereas a non idempotent operation can return a modified state of the resource from one request to another. Hence, in a non idempotent operation, there is no guarantee that one will receive the same state of a resource.



                    Based on the above idempotent definition, my take on using the HTTP PUT method versus using the HTTP POST method for REST services is:
                    Use the HTTP PUT method when:



                    The client includes all aspect of the resource including the unique identifier to uniquely identify the resource. Example: creating a new employee.
                    The client provides all the information for a resource to be able to modify that resource.This implies that the server side does not update any aspect of the resource (such as an update date).


                    In both cases, these operations can be performed multiple times with the same results. That is the resource will not be changed by requesting the operation more than once. Hence, a true idempotent operation.
                    Use the HTTP POST method when:



                    The server will provide some information concerning the newly created resource. For example, take a logging system. A new entry in the log will most likely have a numbering scheme which is determined on the server side. Upon creating a new log entry, the new sequence number will be determined by the server and not by the client.
                    On a modification of a resource, the server will provide such information as a resource state or an update date. Again in this case not all information was provided by the client and the resource will be changing from one modification request to the next. Hence a non idempotent operation.


                    Conclusion



                    Do not directly correlate and map CRUD operations to HTTP methods for REST services. The use of an HTTP PUT method versus an HTTP POST method should be based on the idempotent aspect of that operation. That is, if the operation is idempotent, then use the HTTP PUT method. If the operation is non idempotent, then use the HTTP POST method.






                    share|improve this answer













                    There seems to always be some confusion as to when to use the HTTP POST versus the HTTP PUT method for REST services. Most developers will try to associate CRUD operations directly to HTTP methods. I will argue that this is not correct and one can not simply associate the CRUD concepts to the HTTP methods. That is:



                    Create => HTTP PUT
                    Retrieve => HTTP GET
                    Update => HTTP POST
                    Delete => HTTP DELETE


                    It is true that the R(etrieve) and D(elete) of the CRUD operations can be mapped directly to the HTTP methods GET and DELETE respectively. However, the confusion lies in the C(reate) and U(update) operations. In some cases, one can use the PUT for a create while in other cases a POST will be required. The ambiguity lies in the definition of an HTTP PUT method versus an HTTP POST method.



                    According to the HTTP 1.1 specifications the GET, HEAD, DELETE, and PUT methods must be idempotent, and the POST method is not idempotent. That is to say that an operation is idempotent if it can be performed on a resource once or many times and always return the same state of that resource. Whereas a non idempotent operation can return a modified state of the resource from one request to another. Hence, in a non idempotent operation, there is no guarantee that one will receive the same state of a resource.



                    Based on the above idempotent definition, my take on using the HTTP PUT method versus using the HTTP POST method for REST services is:
                    Use the HTTP PUT method when:



                    The client includes all aspect of the resource including the unique identifier to uniquely identify the resource. Example: creating a new employee.
                    The client provides all the information for a resource to be able to modify that resource.This implies that the server side does not update any aspect of the resource (such as an update date).


                    In both cases, these operations can be performed multiple times with the same results. That is the resource will not be changed by requesting the operation more than once. Hence, a true idempotent operation.
                    Use the HTTP POST method when:



                    The server will provide some information concerning the newly created resource. For example, take a logging system. A new entry in the log will most likely have a numbering scheme which is determined on the server side. Upon creating a new log entry, the new sequence number will be determined by the server and not by the client.
                    On a modification of a resource, the server will provide such information as a resource state or an update date. Again in this case not all information was provided by the client and the resource will be changing from one modification request to the next. Hence a non idempotent operation.


                    Conclusion



                    Do not directly correlate and map CRUD operations to HTTP methods for REST services. The use of an HTTP PUT method versus an HTTP POST method should be based on the idempotent aspect of that operation. That is, if the operation is idempotent, then use the HTTP PUT method. If the operation is non idempotent, then use the HTTP POST method.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Oct 10 '13 at 4:18









                    BurhanBurhan

                    19338




                    19338








                    • 2





                      Update => HTTP POST : POST is not for updating

                      – Premraj
                      Jan 30 '16 at 0:57
















                    • 2





                      Update => HTTP POST : POST is not for updating

                      – Premraj
                      Jan 30 '16 at 0:57










                    2




                    2





                    Update => HTTP POST : POST is not for updating

                    – Premraj
                    Jan 30 '16 at 0:57







                    Update => HTTP POST : POST is not for updating

                    – Premraj
                    Jan 30 '16 at 0:57













                    13















                    the origin server can create the resource with that URI




                    So you use POST and probably, but not necessary PUT for resource creation. You don't have to support both. For me POST is perfectly enough. So it is a design decision.



                    As your quote mentioned, you use PUT for creation of there is no resource assigned to an IRI, and you want to create a resource anyway. For example, PUT /users/123/password usually replaces the old password with a new one, but you can use it to create a password if it does not exist already (for example, by freshly registered users or by restoring banned users).






                    share|improve this answer


























                    • I think you've managed to provide one of the few good examples of how to use PUT, well done.

                      – thecoshman
                      Jun 8 '15 at 8:13
















                    13















                    the origin server can create the resource with that URI




                    So you use POST and probably, but not necessary PUT for resource creation. You don't have to support both. For me POST is perfectly enough. So it is a design decision.



                    As your quote mentioned, you use PUT for creation of there is no resource assigned to an IRI, and you want to create a resource anyway. For example, PUT /users/123/password usually replaces the old password with a new one, but you can use it to create a password if it does not exist already (for example, by freshly registered users or by restoring banned users).






                    share|improve this answer


























                    • I think you've managed to provide one of the few good examples of how to use PUT, well done.

                      – thecoshman
                      Jun 8 '15 at 8:13














                    13












                    13








                    13








                    the origin server can create the resource with that URI




                    So you use POST and probably, but not necessary PUT for resource creation. You don't have to support both. For me POST is perfectly enough. So it is a design decision.



                    As your quote mentioned, you use PUT for creation of there is no resource assigned to an IRI, and you want to create a resource anyway. For example, PUT /users/123/password usually replaces the old password with a new one, but you can use it to create a password if it does not exist already (for example, by freshly registered users or by restoring banned users).






                    share|improve this answer
















                    the origin server can create the resource with that URI




                    So you use POST and probably, but not necessary PUT for resource creation. You don't have to support both. For me POST is perfectly enough. So it is a design decision.



                    As your quote mentioned, you use PUT for creation of there is no resource assigned to an IRI, and you want to create a resource anyway. For example, PUT /users/123/password usually replaces the old password with a new one, but you can use it to create a password if it does not exist already (for example, by freshly registered users or by restoring banned users).







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Oct 21 '17 at 12:41









                    Peter Mortensen

                    13.7k1986111




                    13.7k1986111










                    answered Jan 16 '14 at 7:58









                    inf3rnoinf3rno

                    14.6k775125




                    14.6k775125













                    • I think you've managed to provide one of the few good examples of how to use PUT, well done.

                      – thecoshman
                      Jun 8 '15 at 8:13



















                    • I think you've managed to provide one of the few good examples of how to use PUT, well done.

                      – thecoshman
                      Jun 8 '15 at 8:13

















                    I think you've managed to provide one of the few good examples of how to use PUT, well done.

                    – thecoshman
                    Jun 8 '15 at 8:13





                    I think you've managed to provide one of the few good examples of how to use PUT, well done.

                    – thecoshman
                    Jun 8 '15 at 8:13











                    13














                    Readers new to this topic will be struck by the endless discussion about what you should do, and the relative absence of lessons from experience. The fact that REST is "preferred" over SOAP is, I suppose, a high-level learning from experience, but goodness we must have progressed from there? It's 2016. Roy's dissertation was in 2000. What have we developed? Was it fun? Was it easy to integrate with? To support? Will it handle the rise of smartphones and flaky mobile connections?



                    According to ME, real-life networks are unreliable. Requests timeout. Connections are reset. Networks go down for hours or days at a time. Trains go into tunnels with mobile users aboard. For any given request (as occasionally acknowledged in all this discussion) the request can fall in the water on its way, or the response can fall in the water on its way back. In these conditions, issuing PUT, POST and DELETE requests directly against substantive resources has always struck me as a little brutal and naive.



                    HTTP does nothing to ensure reliable completion of the request-response, and that's just fine because this is properly the job of network-aware applications. Developing such an application, you can jump through hoops to use PUT instead of POST, then more hoops to give a certain kind of error on the server if you detect duplicate requests. Back at the client, you then have to jump through hoops to interpret these errors, refetch, revalidate and repost.



                    Or you can do this: consider your unsafe requests as ephemeral single-user resources (let's call them actions). Clients request a new "action" on a substantive resource with an empty POST to the resource. POST will be used only for this. Once safely in possession of the URI of the freshly minted action, the client PUTs the unsafe request to the action URI, not the target resource. Resolving the action and updating the "real" resource is properly the job of your API, and is here decoupled from the unreliable network.



                    The server does the business, returns the response and stores it against the agreed action URI. If anything goes wrong, the client repeats the request (natural behaviour!), and if the server has already seen it, it repeats the stored response and does nothing else.



                    You will quickly spot the similarity with promises: we create and return the placeholder for the result before doing anything. Also like a promise, an action can succeed or fail one time, but its result can be fetched repeatedly.



                    Best of all, we give sending and receiving applications a chance to link the uniquely identified action to uniqueness in their respective environments. And we can start to demand, and enforce!, responsible behaviour from clients: repeat your requests as much as you like, but don't go generating a new action until you're in possession of a definitive result from the existing one.



                    As such, numerous thorny problems go away. Repeated insert requests won't create duplicates, and we don't create the real resource until we're in possession of the data. (database columns can stay not-nullable). Repeated update requests won't hit incompatible states and won't overwrite subsequent changes. Clients can (re)fetch and seamlessy process the original confirmation for whatever reason (client crashed, response went missing, etc.).



                    Successive delete requests can see and process the original confirmation, without hitting a 404 error. If things take longer than expected, we can respond provisionally, and we have a place where the client can check back for the definitive result. The nicest part of this pattern is its Kung-Fu (Panda) property. We take a weakness, the propensity for clients to repeat a request any time they don't understand the response, and turn it into a strength :-)



                    Before telling me this is not RESTful, please consider the numerous ways in which REST principles are respected. Clients don't construct URLs. The API stays discoverable, albeit with a little change in semantics. HTTP verbs are used appropriately. If you think this is a huge change to implement, I can tell you from experience that it's not.



                    If you think you'll have huge amounts of data to store, let's talk volumes: a typical update confirmation is a fraction of a kilobyte. HTTP currently gives you a minute or two to respond definitively. Even if you only store actions for a week, clients have ample chance to catch up. If you have very high volumes, you may want a dedicated acid-compliant key value store, or an in-memory solution.






                    share|improve this answer


























                    • Wont storing response be like maintaining a session? Which would cause (horizontal) scaling issues.

                      – Saurabh Harwande
                      Aug 21 '18 at 17:57


















                    13














                    Readers new to this topic will be struck by the endless discussion about what you should do, and the relative absence of lessons from experience. The fact that REST is "preferred" over SOAP is, I suppose, a high-level learning from experience, but goodness we must have progressed from there? It's 2016. Roy's dissertation was in 2000. What have we developed? Was it fun? Was it easy to integrate with? To support? Will it handle the rise of smartphones and flaky mobile connections?



                    According to ME, real-life networks are unreliable. Requests timeout. Connections are reset. Networks go down for hours or days at a time. Trains go into tunnels with mobile users aboard. For any given request (as occasionally acknowledged in all this discussion) the request can fall in the water on its way, or the response can fall in the water on its way back. In these conditions, issuing PUT, POST and DELETE requests directly against substantive resources has always struck me as a little brutal and naive.



                    HTTP does nothing to ensure reliable completion of the request-response, and that's just fine because this is properly the job of network-aware applications. Developing such an application, you can jump through hoops to use PUT instead of POST, then more hoops to give a certain kind of error on the server if you detect duplicate requests. Back at the client, you then have to jump through hoops to interpret these errors, refetch, revalidate and repost.



                    Or you can do this: consider your unsafe requests as ephemeral single-user resources (let's call them actions). Clients request a new "action" on a substantive resource with an empty POST to the resource. POST will be used only for this. Once safely in possession of the URI of the freshly minted action, the client PUTs the unsafe request to the action URI, not the target resource. Resolving the action and updating the "real" resource is properly the job of your API, and is here decoupled from the unreliable network.



                    The server does the business, returns the response and stores it against the agreed action URI. If anything goes wrong, the client repeats the request (natural behaviour!), and if the server has already seen it, it repeats the stored response and does nothing else.



                    You will quickly spot the similarity with promises: we create and return the placeholder for the result before doing anything. Also like a promise, an action can succeed or fail one time, but its result can be fetched repeatedly.



                    Best of all, we give sending and receiving applications a chance to link the uniquely identified action to uniqueness in their respective environments. And we can start to demand, and enforce!, responsible behaviour from clients: repeat your requests as much as you like, but don't go generating a new action until you're in possession of a definitive result from the existing one.



                    As such, numerous thorny problems go away. Repeated insert requests won't create duplicates, and we don't create the real resource until we're in possession of the data. (database columns can stay not-nullable). Repeated update requests won't hit incompatible states and won't overwrite subsequent changes. Clients can (re)fetch and seamlessy process the original confirmation for whatever reason (client crashed, response went missing, etc.).



                    Successive delete requests can see and process the original confirmation, without hitting a 404 error. If things take longer than expected, we can respond provisionally, and we have a place where the client can check back for the definitive result. The nicest part of this pattern is its Kung-Fu (Panda) property. We take a weakness, the propensity for clients to repeat a request any time they don't understand the response, and turn it into a strength :-)



                    Before telling me this is not RESTful, please consider the numerous ways in which REST principles are respected. Clients don't construct URLs. The API stays discoverable, albeit with a little change in semantics. HTTP verbs are used appropriately. If you think this is a huge change to implement, I can tell you from experience that it's not.



                    If you think you'll have huge amounts of data to store, let's talk volumes: a typical update confirmation is a fraction of a kilobyte. HTTP currently gives you a minute or two to respond definitively. Even if you only store actions for a week, clients have ample chance to catch up. If you have very high volumes, you may want a dedicated acid-compliant key value store, or an in-memory solution.






                    share|improve this answer


























                    • Wont storing response be like maintaining a session? Which would cause (horizontal) scaling issues.

                      – Saurabh Harwande
                      Aug 21 '18 at 17:57
















                    13












                    13








                    13







                    Readers new to this topic will be struck by the endless discussion about what you should do, and the relative absence of lessons from experience. The fact that REST is "preferred" over SOAP is, I suppose, a high-level learning from experience, but goodness we must have progressed from there? It's 2016. Roy's dissertation was in 2000. What have we developed? Was it fun? Was it easy to integrate with? To support? Will it handle the rise of smartphones and flaky mobile connections?



                    According to ME, real-life networks are unreliable. Requests timeout. Connections are reset. Networks go down for hours or days at a time. Trains go into tunnels with mobile users aboard. For any given request (as occasionally acknowledged in all this discussion) the request can fall in the water on its way, or the response can fall in the water on its way back. In these conditions, issuing PUT, POST and DELETE requests directly against substantive resources has always struck me as a little brutal and naive.



                    HTTP does nothing to ensure reliable completion of the request-response, and that's just fine because this is properly the job of network-aware applications. Developing such an application, you can jump through hoops to use PUT instead of POST, then more hoops to give a certain kind of error on the server if you detect duplicate requests. Back at the client, you then have to jump through hoops to interpret these errors, refetch, revalidate and repost.



                    Or you can do this: consider your unsafe requests as ephemeral single-user resources (let's call them actions). Clients request a new "action" on a substantive resource with an empty POST to the resource. POST will be used only for this. Once safely in possession of the URI of the freshly minted action, the client PUTs the unsafe request to the action URI, not the target resource. Resolving the action and updating the "real" resource is properly the job of your API, and is here decoupled from the unreliable network.



                    The server does the business, returns the response and stores it against the agreed action URI. If anything goes wrong, the client repeats the request (natural behaviour!), and if the server has already seen it, it repeats the stored response and does nothing else.



                    You will quickly spot the similarity with promises: we create and return the placeholder for the result before doing anything. Also like a promise, an action can succeed or fail one time, but its result can be fetched repeatedly.



                    Best of all, we give sending and receiving applications a chance to link the uniquely identified action to uniqueness in their respective environments. And we can start to demand, and enforce!, responsible behaviour from clients: repeat your requests as much as you like, but don't go generating a new action until you're in possession of a definitive result from the existing one.



                    As such, numerous thorny problems go away. Repeated insert requests won't create duplicates, and we don't create the real resource until we're in possession of the data. (database columns can stay not-nullable). Repeated update requests won't hit incompatible states and won't overwrite subsequent changes. Clients can (re)fetch and seamlessy process the original confirmation for whatever reason (client crashed, response went missing, etc.).



                    Successive delete requests can see and process the original confirmation, without hitting a 404 error. If things take longer than expected, we can respond provisionally, and we have a place where the client can check back for the definitive result. The nicest part of this pattern is its Kung-Fu (Panda) property. We take a weakness, the propensity for clients to repeat a request any time they don't understand the response, and turn it into a strength :-)



                    Before telling me this is not RESTful, please consider the numerous ways in which REST principles are respected. Clients don't construct URLs. The API stays discoverable, albeit with a little change in semantics. HTTP verbs are used appropriately. If you think this is a huge change to implement, I can tell you from experience that it's not.



                    If you think you'll have huge amounts of data to store, let's talk volumes: a typical update confirmation is a fraction of a kilobyte. HTTP currently gives you a minute or two to respond definitively. Even if you only store actions for a week, clients have ample chance to catch up. If you have very high volumes, you may want a dedicated acid-compliant key value store, or an in-memory solution.






                    share|improve this answer















                    Readers new to this topic will be struck by the endless discussion about what you should do, and the relative absence of lessons from experience. The fact that REST is "preferred" over SOAP is, I suppose, a high-level learning from experience, but goodness we must have progressed from there? It's 2016. Roy's dissertation was in 2000. What have we developed? Was it fun? Was it easy to integrate with? To support? Will it handle the rise of smartphones and flaky mobile connections?



                    According to ME, real-life networks are unreliable. Requests timeout. Connections are reset. Networks go down for hours or days at a time. Trains go into tunnels with mobile users aboard. For any given request (as occasionally acknowledged in all this discussion) the request can fall in the water on its way, or the response can fall in the water on its way back. In these conditions, issuing PUT, POST and DELETE requests directly against substantive resources has always struck me as a little brutal and naive.



                    HTTP does nothing to ensure reliable completion of the request-response, and that's just fine because this is properly the job of network-aware applications. Developing such an application, you can jump through hoops to use PUT instead of POST, then more hoops to give a certain kind of error on the server if you detect duplicate requests. Back at the client, you then have to jump through hoops to interpret these errors, refetch, revalidate and repost.



                    Or you can do this: consider your unsafe requests as ephemeral single-user resources (let's call them actions). Clients request a new "action" on a substantive resource with an empty POST to the resource. POST will be used only for this. Once safely in possession of the URI of the freshly minted action, the client PUTs the unsafe request to the action URI, not the target resource. Resolving the action and updating the "real" resource is properly the job of your API, and is here decoupled from the unreliable network.



                    The server does the business, returns the response and stores it against the agreed action URI. If anything goes wrong, the client repeats the request (natural behaviour!), and if the server has already seen it, it repeats the stored response and does nothing else.



                    You will quickly spot the similarity with promises: we create and return the placeholder for the result before doing anything. Also like a promise, an action can succeed or fail one time, but its result can be fetched repeatedly.



                    Best of all, we give sending and receiving applications a chance to link the uniquely identified action to uniqueness in their respective environments. And we can start to demand, and enforce!, responsible behaviour from clients: repeat your requests as much as you like, but don't go generating a new action until you're in possession of a definitive result from the existing one.



                    As such, numerous thorny problems go away. Repeated insert requests won't create duplicates, and we don't create the real resource until we're in possession of the data. (database columns can stay not-nullable). Repeated update requests won't hit incompatible states and won't overwrite subsequent changes. Clients can (re)fetch and seamlessy process the original confirmation for whatever reason (client crashed, response went missing, etc.).



                    Successive delete requests can see and process the original confirmation, without hitting a 404 error. If things take longer than expected, we can respond provisionally, and we have a place where the client can check back for the definitive result. The nicest part of this pattern is its Kung-Fu (Panda) property. We take a weakness, the propensity for clients to repeat a request any time they don't understand the response, and turn it into a strength :-)



                    Before telling me this is not RESTful, please consider the numerous ways in which REST principles are respected. Clients don't construct URLs. The API stays discoverable, albeit with a little change in semantics. HTTP verbs are used appropriately. If you think this is a huge change to implement, I can tell you from experience that it's not.



                    If you think you'll have huge amounts of data to store, let's talk volumes: a typical update confirmation is a fraction of a kilobyte. HTTP currently gives you a minute or two to respond definitively. Even if you only store actions for a week, clients have ample chance to catch up. If you have very high volumes, you may want a dedicated acid-compliant key value store, or an in-memory solution.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited May 23 '18 at 14:06

























                    answered Feb 18 '16 at 11:45









                    bbsimonbbbbsimonbb

                    9,86463446




                    9,86463446













                    • Wont storing response be like maintaining a session? Which would cause (horizontal) scaling issues.

                      – Saurabh Harwande
                      Aug 21 '18 at 17:57





















                    • Wont storing response be like maintaining a session? Which would cause (horizontal) scaling issues.

                      – Saurabh Harwande
                      Aug 21 '18 at 17:57



















                    Wont storing response be like maintaining a session? Which would cause (horizontal) scaling issues.

                    – Saurabh Harwande
                    Aug 21 '18 at 17:57







                    Wont storing response be like maintaining a session? Which would cause (horizontal) scaling issues.

                    – Saurabh Harwande
                    Aug 21 '18 at 17:57













                    11














                    I'm going to land with the following:



                    PUT refers to a resource, identified by the URI. In this case, you are updating it. It is the part of the three verbs referring to resources -- delete and get being the other two.



                    POST is basically a free form message, with its meaning being defined 'out of band'. If the message can be interpreted as adding a resource to a directory, that would be OK, but basically you need to understand the message you are sending (posting) to know what will happen with the resource.





                    Because PUT and GET and DELETE refer to a resource, they are also by definition idempotent.



                    POST can perform the other three functions, but then the semantics of the request will be lost on the intermediaries such as caches and proxies. This also applies to providing security on the resource, since a post's URI doesn't necessarily indicate the resource it is applying to (it can though).



                    A PUT doesn't need to be a create; the service could error if the resource isn't already created, but otherwise update it. Or vice versa -- it may create the resource, but not allow updates. The only thing required about PUT is that it points to a specific resource, and its payload is the representation of that resource. A successful PUT means (barring interference) that a GET would retrieve the same resource.





                    Edit: One more thing -- a PUT can create, but if it does then the ID has to be a natural ID -- AKA an email address. That way when you PUT twice, the second put is an update of the first. This makes it idempotent.



                    If the ID is generated (a new employee ID, for example), then the second PUT with the same URL would create a new record, which violates the idempotent rule. In this case the verb would be POST, and the message (not resource) would be to create a resource using the values defined in this message.






                    share|improve this answer






























                      11














                      I'm going to land with the following:



                      PUT refers to a resource, identified by the URI. In this case, you are updating it. It is the part of the three verbs referring to resources -- delete and get being the other two.



                      POST is basically a free form message, with its meaning being defined 'out of band'. If the message can be interpreted as adding a resource to a directory, that would be OK, but basically you need to understand the message you are sending (posting) to know what will happen with the resource.





                      Because PUT and GET and DELETE refer to a resource, they are also by definition idempotent.



                      POST can perform the other three functions, but then the semantics of the request will be lost on the intermediaries such as caches and proxies. This also applies to providing security on the resource, since a post's URI doesn't necessarily indicate the resource it is applying to (it can though).



                      A PUT doesn't need to be a create; the service could error if the resource isn't already created, but otherwise update it. Or vice versa -- it may create the resource, but not allow updates. The only thing required about PUT is that it points to a specific resource, and its payload is the representation of that resource. A successful PUT means (barring interference) that a GET would retrieve the same resource.





                      Edit: One more thing -- a PUT can create, but if it does then the ID has to be a natural ID -- AKA an email address. That way when you PUT twice, the second put is an update of the first. This makes it idempotent.



                      If the ID is generated (a new employee ID, for example), then the second PUT with the same URL would create a new record, which violates the idempotent rule. In this case the verb would be POST, and the message (not resource) would be to create a resource using the values defined in this message.






                      share|improve this answer




























                        11












                        11








                        11







                        I'm going to land with the following:



                        PUT refers to a resource, identified by the URI. In this case, you are updating it. It is the part of the three verbs referring to resources -- delete and get being the other two.



                        POST is basically a free form message, with its meaning being defined 'out of band'. If the message can be interpreted as adding a resource to a directory, that would be OK, but basically you need to understand the message you are sending (posting) to know what will happen with the resource.





                        Because PUT and GET and DELETE refer to a resource, they are also by definition idempotent.



                        POST can perform the other three functions, but then the semantics of the request will be lost on the intermediaries such as caches and proxies. This also applies to providing security on the resource, since a post's URI doesn't necessarily indicate the resource it is applying to (it can though).



                        A PUT doesn't need to be a create; the service could error if the resource isn't already created, but otherwise update it. Or vice versa -- it may create the resource, but not allow updates. The only thing required about PUT is that it points to a specific resource, and its payload is the representation of that resource. A successful PUT means (barring interference) that a GET would retrieve the same resource.





                        Edit: One more thing -- a PUT can create, but if it does then the ID has to be a natural ID -- AKA an email address. That way when you PUT twice, the second put is an update of the first. This makes it idempotent.



                        If the ID is generated (a new employee ID, for example), then the second PUT with the same URL would create a new record, which violates the idempotent rule. In this case the verb would be POST, and the message (not resource) would be to create a resource using the values defined in this message.






                        share|improve this answer















                        I'm going to land with the following:



                        PUT refers to a resource, identified by the URI. In this case, you are updating it. It is the part of the three verbs referring to resources -- delete and get being the other two.



                        POST is basically a free form message, with its meaning being defined 'out of band'. If the message can be interpreted as adding a resource to a directory, that would be OK, but basically you need to understand the message you are sending (posting) to know what will happen with the resource.





                        Because PUT and GET and DELETE refer to a resource, they are also by definition idempotent.



                        POST can perform the other three functions, but then the semantics of the request will be lost on the intermediaries such as caches and proxies. This also applies to providing security on the resource, since a post's URI doesn't necessarily indicate the resource it is applying to (it can though).



                        A PUT doesn't need to be a create; the service could error if the resource isn't already created, but otherwise update it. Or vice versa -- it may create the resource, but not allow updates. The only thing required about PUT is that it points to a specific resource, and its payload is the representation of that resource. A successful PUT means (barring interference) that a GET would retrieve the same resource.





                        Edit: One more thing -- a PUT can create, but if it does then the ID has to be a natural ID -- AKA an email address. That way when you PUT twice, the second put is an update of the first. This makes it idempotent.



                        If the ID is generated (a new employee ID, for example), then the second PUT with the same URL would create a new record, which violates the idempotent rule. In this case the verb would be POST, and the message (not resource) would be to create a resource using the values defined in this message.







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Oct 21 '17 at 12:32









                        Peter Mortensen

                        13.7k1986111




                        13.7k1986111










                        answered Oct 21 '13 at 21:16









                        Gerard ONeillGerard ONeill

                        2,3752317




                        2,3752317























                            9














                            The semantics are supposed be different, in that "PUT", like "GET" is supposed to be idempotent -- meaning, you can the same exact PUT request multiple times and the result will be as if you executed it only once.



                            I will describe the conventions which I think are most widely used and are most useful:



                            When you PUT a resource at a particular URL what happens is that it should get saved at that URL, or something along those lines.



                            When you POST to a resource at a particular URL, often you are posting a related piece of information to that URL. This implies that the resource at the URL already exists.



                            For example, when you want to create a new stream, you can PUT it to some URL. But when you want to POST a message to an existing stream, you POST to its URL.



                            As for modifying the properties of the stream, you can do that with either PUT or POST. Basically, only use "PUT" when the operation is idempotent - otherwise use POST.



                            Note, however, that not all modern browsers support HTTP verbs other than GET or POST.






                            share|improve this answer
























                            • What you describe POST as is actually how PATCH should behave. POST is supposed to mean something more akin to "append" as in "post to mailing list".

                              – Alexander Torstling
                              Nov 28 '14 at 15:57
















                            9














                            The semantics are supposed be different, in that "PUT", like "GET" is supposed to be idempotent -- meaning, you can the same exact PUT request multiple times and the result will be as if you executed it only once.



                            I will describe the conventions which I think are most widely used and are most useful:



                            When you PUT a resource at a particular URL what happens is that it should get saved at that URL, or something along those lines.



                            When you POST to a resource at a particular URL, often you are posting a related piece of information to that URL. This implies that the resource at the URL already exists.



                            For example, when you want to create a new stream, you can PUT it to some URL. But when you want to POST a message to an existing stream, you POST to its URL.



                            As for modifying the properties of the stream, you can do that with either PUT or POST. Basically, only use "PUT" when the operation is idempotent - otherwise use POST.



                            Note, however, that not all modern browsers support HTTP verbs other than GET or POST.






                            share|improve this answer
























                            • What you describe POST as is actually how PATCH should behave. POST is supposed to mean something more akin to "append" as in "post to mailing list".

                              – Alexander Torstling
                              Nov 28 '14 at 15:57














                            9












                            9








                            9







                            The semantics are supposed be different, in that "PUT", like "GET" is supposed to be idempotent -- meaning, you can the same exact PUT request multiple times and the result will be as if you executed it only once.



                            I will describe the conventions which I think are most widely used and are most useful:



                            When you PUT a resource at a particular URL what happens is that it should get saved at that URL, or something along those lines.



                            When you POST to a resource at a particular URL, often you are posting a related piece of information to that URL. This implies that the resource at the URL already exists.



                            For example, when you want to create a new stream, you can PUT it to some URL. But when you want to POST a message to an existing stream, you POST to its URL.



                            As for modifying the properties of the stream, you can do that with either PUT or POST. Basically, only use "PUT" when the operation is idempotent - otherwise use POST.



                            Note, however, that not all modern browsers support HTTP verbs other than GET or POST.






                            share|improve this answer













                            The semantics are supposed be different, in that "PUT", like "GET" is supposed to be idempotent -- meaning, you can the same exact PUT request multiple times and the result will be as if you executed it only once.



                            I will describe the conventions which I think are most widely used and are most useful:



                            When you PUT a resource at a particular URL what happens is that it should get saved at that URL, or something along those lines.



                            When you POST to a resource at a particular URL, often you are posting a related piece of information to that URL. This implies that the resource at the URL already exists.



                            For example, when you want to create a new stream, you can PUT it to some URL. But when you want to POST a message to an existing stream, you POST to its URL.



                            As for modifying the properties of the stream, you can do that with either PUT or POST. Basically, only use "PUT" when the operation is idempotent - otherwise use POST.



                            Note, however, that not all modern browsers support HTTP verbs other than GET or POST.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Oct 23 '11 at 20:07









                            Gregory MagarshakGregory Magarshak

                            6941222




                            6941222













                            • What you describe POST as is actually how PATCH should behave. POST is supposed to mean something more akin to "append" as in "post to mailing list".

                              – Alexander Torstling
                              Nov 28 '14 at 15:57



















                            • What you describe POST as is actually how PATCH should behave. POST is supposed to mean something more akin to "append" as in "post to mailing list".

                              – Alexander Torstling
                              Nov 28 '14 at 15:57

















                            What you describe POST as is actually how PATCH should behave. POST is supposed to mean something more akin to "append" as in "post to mailing list".

                            – Alexander Torstling
                            Nov 28 '14 at 15:57





                            What you describe POST as is actually how PATCH should behave. POST is supposed to mean something more akin to "append" as in "post to mailing list".

                            – Alexander Torstling
                            Nov 28 '14 at 15:57











                            7














                            Most of the time, you will use them like this:





                            • POST a resource into a collection


                            • PUT a resource identified by collection/:id


                            For example:





                            • POST /items


                            • PUT /items/1234


                            In both cases, the request body contains the data for the resource to be created or updated. It should be obvious from the route names that POST is not idempotent (if you call it 3 times it will create 3 objects), but PUT is idempotent (if you call it 3 times the result is the same). PUT is often used for "upsert" operation (create or update), but you can always return a 404 error if you only want to use it to modify.



                            Note that POST "creates" a new element in the collection, and PUT "replaces" an element at a given URL, but it is a very common practice to use PUT for partial modifications, that is, use it only to update existing resources and only modify the included fields in the body (ignoring the other fields). This is technically incorrect, if you want to be REST-purist, PUT should replace the whole resource and you should use PATCH for the partial update. I personally don't care much as far as the behavior is clear and consistent across all your API endpoints.



                            Remember, REST is a set of conventions and guidelines to keep your API simple. If you end up with a complicated work-around just to check the "RESTfull" box then you are defeating the purpose ;)






                            share|improve this answer




























                              7














                              Most of the time, you will use them like this:





                              • POST a resource into a collection


                              • PUT a resource identified by collection/:id


                              For example:





                              • POST /items


                              • PUT /items/1234


                              In both cases, the request body contains the data for the resource to be created or updated. It should be obvious from the route names that POST is not idempotent (if you call it 3 times it will create 3 objects), but PUT is idempotent (if you call it 3 times the result is the same). PUT is often used for "upsert" operation (create or update), but you can always return a 404 error if you only want to use it to modify.



                              Note that POST "creates" a new element in the collection, and PUT "replaces" an element at a given URL, but it is a very common practice to use PUT for partial modifications, that is, use it only to update existing resources and only modify the included fields in the body (ignoring the other fields). This is technically incorrect, if you want to be REST-purist, PUT should replace the whole resource and you should use PATCH for the partial update. I personally don't care much as far as the behavior is clear and consistent across all your API endpoints.



                              Remember, REST is a set of conventions and guidelines to keep your API simple. If you end up with a complicated work-around just to check the "RESTfull" box then you are defeating the purpose ;)






                              share|improve this answer


























                                7












                                7








                                7







                                Most of the time, you will use them like this:





                                • POST a resource into a collection


                                • PUT a resource identified by collection/:id


                                For example:





                                • POST /items


                                • PUT /items/1234


                                In both cases, the request body contains the data for the resource to be created or updated. It should be obvious from the route names that POST is not idempotent (if you call it 3 times it will create 3 objects), but PUT is idempotent (if you call it 3 times the result is the same). PUT is often used for "upsert" operation (create or update), but you can always return a 404 error if you only want to use it to modify.



                                Note that POST "creates" a new element in the collection, and PUT "replaces" an element at a given URL, but it is a very common practice to use PUT for partial modifications, that is, use it only to update existing resources and only modify the included fields in the body (ignoring the other fields). This is technically incorrect, if you want to be REST-purist, PUT should replace the whole resource and you should use PATCH for the partial update. I personally don't care much as far as the behavior is clear and consistent across all your API endpoints.



                                Remember, REST is a set of conventions and guidelines to keep your API simple. If you end up with a complicated work-around just to check the "RESTfull" box then you are defeating the purpose ;)






                                share|improve this answer













                                Most of the time, you will use them like this:





                                • POST a resource into a collection


                                • PUT a resource identified by collection/:id


                                For example:





                                • POST /items


                                • PUT /items/1234


                                In both cases, the request body contains the data for the resource to be created or updated. It should be obvious from the route names that POST is not idempotent (if you call it 3 times it will create 3 objects), but PUT is idempotent (if you call it 3 times the result is the same). PUT is often used for "upsert" operation (create or update), but you can always return a 404 error if you only want to use it to modify.



                                Note that POST "creates" a new element in the collection, and PUT "replaces" an element at a given URL, but it is a very common practice to use PUT for partial modifications, that is, use it only to update existing resources and only modify the included fields in the body (ignoring the other fields). This is technically incorrect, if you want to be REST-purist, PUT should replace the whole resource and you should use PATCH for the partial update. I personally don't care much as far as the behavior is clear and consistent across all your API endpoints.



                                Remember, REST is a set of conventions and guidelines to keep your API simple. If you end up with a complicated work-around just to check the "RESTfull" box then you are defeating the purpose ;)







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Jun 21 '17 at 17:38









                                tothemariotothemario

                                3,43123131




                                3,43123131























                                    7














                                    While there is probably an agnostic way to describe these, it does seem to be conflicting with various statements from answers to websites.



                                    Let's be very clear and direct here. If you are a .NET developer working with Web API, the facts are (from the Microsoft API documentation),
                                    http://www.asp.net/web-api/overview/creating-web-apis/creating-a-web-api-that-supports-crud-operations:



                                    1. PUT = UPDATE (/api/products/id)
                                    2. MCSD Exams 2014 - UPDATE = PUT, there are **NO** multiple answers for that question period.


                                    Sure you "can" use "POST" to update, but just follow the conventions laid out for you with your given framework. In my case it is .NET / Web API, so PUT is for UPDATE there is no debate.



                                    I hope this helps any Microsoft developers that read all comments with Amazon and Sun/Java website links.






                                    share|improve this answer






























                                      7














                                      While there is probably an agnostic way to describe these, it does seem to be conflicting with various statements from answers to websites.



                                      Let's be very clear and direct here. If you are a .NET developer working with Web API, the facts are (from the Microsoft API documentation),
                                      http://www.asp.net/web-api/overview/creating-web-apis/creating-a-web-api-that-supports-crud-operations:



                                      1. PUT = UPDATE (/api/products/id)
                                      2. MCSD Exams 2014 - UPDATE = PUT, there are **NO** multiple answers for that question period.


                                      Sure you "can" use "POST" to update, but just follow the conventions laid out for you with your given framework. In my case it is .NET / Web API, so PUT is for UPDATE there is no debate.



                                      I hope this helps any Microsoft developers that read all comments with Amazon and Sun/Java website links.






                                      share|improve this answer




























                                        7












                                        7








                                        7







                                        While there is probably an agnostic way to describe these, it does seem to be conflicting with various statements from answers to websites.



                                        Let's be very clear and direct here. If you are a .NET developer working with Web API, the facts are (from the Microsoft API documentation),
                                        http://www.asp.net/web-api/overview/creating-web-apis/creating-a-web-api-that-supports-crud-operations:



                                        1. PUT = UPDATE (/api/products/id)
                                        2. MCSD Exams 2014 - UPDATE = PUT, there are **NO** multiple answers for that question period.


                                        Sure you "can" use "POST" to update, but just follow the conventions laid out for you with your given framework. In my case it is .NET / Web API, so PUT is for UPDATE there is no debate.



                                        I hope this helps any Microsoft developers that read all comments with Amazon and Sun/Java website links.






                                        share|improve this answer















                                        While there is probably an agnostic way to describe these, it does seem to be conflicting with various statements from answers to websites.



                                        Let's be very clear and direct here. If you are a .NET developer working with Web API, the facts are (from the Microsoft API documentation),
                                        http://www.asp.net/web-api/overview/creating-web-apis/creating-a-web-api-that-supports-crud-operations:



                                        1. PUT = UPDATE (/api/products/id)
                                        2. MCSD Exams 2014 - UPDATE = PUT, there are **NO** multiple answers for that question period.


                                        Sure you "can" use "POST" to update, but just follow the conventions laid out for you with your given framework. In my case it is .NET / Web API, so PUT is for UPDATE there is no debate.



                                        I hope this helps any Microsoft developers that read all comments with Amazon and Sun/Java website links.







                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited Jul 22 '17 at 11:48









                                        Peter Mortensen

                                        13.7k1986111




                                        13.7k1986111










                                        answered Jul 21 '14 at 20:02









                                        Tom StickelTom Stickel

                                        12.5k48594




                                        12.5k48594























                                            7














                                            In addition to differences suggested by others, I want to add one more.



                                            In POST method you can send body params in form-data



                                            In PUT method you have to send body params in x-www-form-urlencoded



                                            Header Content-Type:application/x-www-form-urlencoded



                                            According to this, you cannot send files or multipart data in the PUT method



                                            EDIT




                                            The content type "application/x-www-form-urlencoded" is inefficient
                                            for sending large quantities of binary data or text containing
                                            non-ASCII characters. The content type "multipart/form-data" should be
                                            used for submitting forms that contain files, non-ASCII data, and
                                            binary data.




                                            Which means if you have to submit




                                            files, non-ASCII data, and binary data




                                            you should use POST method






                                            share|improve this answer





















                                            • 2





                                              Why was this not upvoted? If true, this is a critical distinction is it not?

                                              – Matt1776
                                              Sep 29 '18 at 3:02






                                            • 2





                                              I faced it when implementing API for the profile update, which includes user profile pic upload. Then I tested it with the postman, Ajax, PHP curl and laravel 5.6 as backend.

                                              – Rohit Dhiman
                                              Sep 29 '18 at 6:54
















                                            7














                                            In addition to differences suggested by others, I want to add one more.



                                            In POST method you can send body params in form-data



                                            In PUT method you have to send body params in x-www-form-urlencoded



                                            Header Content-Type:application/x-www-form-urlencoded



                                            According to this, you cannot send files or multipart data in the PUT method



                                            EDIT




                                            The content type "application/x-www-form-urlencoded" is inefficient
                                            for sending large quantities of binary data or text containing
                                            non-ASCII characters. The content type "multipart/form-data" should be
                                            used for submitting forms that contain files, non-ASCII data, and
                                            binary data.




                                            Which means if you have to submit




                                            files, non-ASCII data, and binary data




                                            you should use POST method






                                            share|improve this answer





















                                            • 2





                                              Why was this not upvoted? If true, this is a critical distinction is it not?

                                              – Matt1776
                                              Sep 29 '18 at 3:02






                                            • 2





                                              I faced it when implementing API for the profile update, which includes user profile pic upload. Then I tested it with the postman, Ajax, PHP curl and laravel 5.6 as backend.

                                              – Rohit Dhiman
                                              Sep 29 '18 at 6:54














                                            7












                                            7








                                            7







                                            In addition to differences suggested by others, I want to add one more.



                                            In POST method you can send body params in form-data



                                            In PUT method you have to send body params in x-www-form-urlencoded



                                            Header Content-Type:application/x-www-form-urlencoded



                                            According to this, you cannot send files or multipart data in the PUT method



                                            EDIT




                                            The content type "application/x-www-form-urlencoded" is inefficient
                                            for sending large quantities of binary data or text containing
                                            non-ASCII characters. The content type "multipart/form-data" should be
                                            used for submitting forms that contain files, non-ASCII data, and
                                            binary data.




                                            Which means if you have to submit




                                            files, non-ASCII data, and binary data




                                            you should use POST method






                                            share|improve this answer















                                            In addition to differences suggested by others, I want to add one more.



                                            In POST method you can send body params in form-data



                                            In PUT method you have to send body params in x-www-form-urlencoded



                                            Header Content-Type:application/x-www-form-urlencoded



                                            According to this, you cannot send files or multipart data in the PUT method



                                            EDIT




                                            The content type "application/x-www-form-urlencoded" is inefficient
                                            for sending large quantities of binary data or text containing
                                            non-ASCII characters. The content type "multipart/form-data" should be
                                            used for submitting forms that contain files, non-ASCII data, and
                                            binary data.




                                            Which means if you have to submit




                                            files, non-ASCII data, and binary data




                                            you should use POST method







                                            share|improve this answer














                                            share|improve this answer



                                            share|improve this answer








                                            edited Oct 1 '18 at 9:33

























                                            answered Sep 25 '18 at 8:28









                                            Rohit DhimanRohit Dhiman

                                            1,810727




                                            1,810727








                                            • 2





                                              Why was this not upvoted? If true, this is a critical distinction is it not?

                                              – Matt1776
                                              Sep 29 '18 at 3:02






                                            • 2





                                              I faced it when implementing API for the profile update, which includes user profile pic upload. Then I tested it with the postman, Ajax, PHP curl and laravel 5.6 as backend.

                                              – Rohit Dhiman
                                              Sep 29 '18 at 6:54














                                            • 2





                                              Why was this not upvoted? If true, this is a critical distinction is it not?

                                              – Matt1776
                                              Sep 29 '18 at 3:02






                                            • 2





                                              I faced it when implementing API for the profile update, which includes user profile pic upload. Then I tested it with the postman, Ajax, PHP curl and laravel 5.6 as backend.

                                              – Rohit Dhiman
                                              Sep 29 '18 at 6:54








                                            2




                                            2





                                            Why was this not upvoted? If true, this is a critical distinction is it not?

                                            – Matt1776
                                            Sep 29 '18 at 3:02





                                            Why was this not upvoted? If true, this is a critical distinction is it not?

                                            – Matt1776
                                            Sep 29 '18 at 3:02




                                            2




                                            2





                                            I faced it when implementing API for the profile update, which includes user profile pic upload. Then I tested it with the postman, Ajax, PHP curl and laravel 5.6 as backend.

                                            – Rohit Dhiman
                                            Sep 29 '18 at 6:54





                                            I faced it when implementing API for the profile update, which includes user profile pic upload. Then I tested it with the postman, Ajax, PHP curl and laravel 5.6 as backend.

                                            – Rohit Dhiman
                                            Sep 29 '18 at 6:54











                                            6














                                            If you are familiar with database operations,
                                            there are




                                            1. Select

                                            2. Insert

                                            3. Update

                                            4. Delete

                                            5. Merge (Update if already existing, else insert)


                                            I use PUT for Merge and update like operations and use POST for Insertions.






                                            share|improve this answer




























                                              6














                                              If you are familiar with database operations,
                                              there are




                                              1. Select

                                              2. Insert

                                              3. Update

                                              4. Delete

                                              5. Merge (Update if already existing, else insert)


                                              I use PUT for Merge and update like operations and use POST for Insertions.






                                              share|improve this answer


























                                                6












                                                6








                                                6







                                                If you are familiar with database operations,
                                                there are




                                                1. Select

                                                2. Insert

                                                3. Update

                                                4. Delete

                                                5. Merge (Update if already existing, else insert)


                                                I use PUT for Merge and update like operations and use POST for Insertions.






                                                share|improve this answer













                                                If you are familiar with database operations,
                                                there are




                                                1. Select

                                                2. Insert

                                                3. Update

                                                4. Delete

                                                5. Merge (Update if already existing, else insert)


                                                I use PUT for Merge and update like operations and use POST for Insertions.







                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Jun 29 '16 at 21:13









                                                RajanRajan

                                                97021725




                                                97021725























                                                    6














                                                    Here's a simple rule:



                                                    PUT to a URL should be used to update or create the resource that can be located at that URL.



                                                    POST to a URL should be used to update or create a resource which is located at some other ("subordinate") URL, or is not locatable via HTTP.






                                                    share|improve this answer





















                                                    • 1





                                                      PUT is not for update, it is for replace, note that to create you are replacing nothing with something. POST is absolutely not for update in any shape of form.

                                                      – thecoshman
                                                      Jun 8 '15 at 8:10






                                                    • 1





                                                      Does the http spec say that? Or are you basing your comment on something else?

                                                      – Adam Griffiths
                                                      Jul 10 '16 at 20:41











                                                    • It's just common sense, how you update something when you don't know what it is you are updating? POST is for creating a new resource.

                                                      – thecoshman
                                                      Jul 27 '16 at 9:23






                                                    • 2





                                                      thecoshman -- you are abusing semantics here -- a replace can be an update if it is the same resource with a few differences. A replace is only valid for put if replace is used to change the same resource. Replacing with a new and different resource is invalid (remove old and add new?), especially if the 'new' resource doesn't have a natural ID. POST, OTOH, is something that can create, update, replace, and delete -- using post depends on whether or not there is a message to interpret, such as 'apply the discount', which may or may not change the resource depending on logic.

                                                      – Gerard ONeill
                                                      Dec 28 '16 at 16:54











                                                    • As for your second comment -- how about you 'get' the resource, modify the fields you need to, and then put it back? Or how about if the resource comes from a different source but uses a natural ID (the external ID) -- put would naturally update the resource at the URL when the original data changed.

                                                      – Gerard ONeill
                                                      Dec 28 '16 at 16:56
















                                                    6














                                                    Here's a simple rule:



                                                    PUT to a URL should be used to update or create the resource that can be located at that URL.



                                                    POST to a URL should be used to update or create a resource which is located at some other ("subordinate") URL, or is not locatable via HTTP.






                                                    share|improve this answer





















                                                    • 1





                                                      PUT is not for update, it is for replace, note that to create you are replacing nothing with something. POST is absolutely not for update in any shape of form.

                                                      – thecoshman
                                                      Jun 8 '15 at 8:10






                                                    • 1





                                                      Does the http spec say that? Or are you basing your comment on something else?

                                                      – Adam Griffiths
                                                      Jul 10 '16 at 20:41











                                                    • It's just common sense, how you update something when you don't know what it is you are updating? POST is for creating a new resource.

                                                      – thecoshman
                                                      Jul 27 '16 at 9:23






                                                    • 2





                                                      thecoshman -- you are abusing semantics here -- a replace can be an update if it is the same resource with a few differences. A replace is only valid for put if replace is used to change the same resource. Replacing with a new and different resource is invalid (remove old and add new?), especially if the 'new' resource doesn't have a natural ID. POST, OTOH, is something that can create, update, replace, and delete -- using post depends on whether or not there is a message to interpret, such as 'apply the discount', which may or may not change the resource depending on logic.

                                                      – Gerard ONeill
                                                      Dec 28 '16 at 16:54











                                                    • As for your second comment -- how about you 'get' the resource, modify the fields you need to, and then put it back? Or how about if the resource comes from a different source but uses a natural ID (the external ID) -- put would naturally update the resource at the URL when the original data changed.

                                                      – Gerard ONeill
                                                      Dec 28 '16 at 16:56














                                                    6












                                                    6








                                                    6







                                                    Here's a simple rule:



                                                    PUT to a URL should be used to update or create the resource that can be located at that URL.



                                                    POST to a URL should be used to update or create a resource which is located at some other ("subordinate") URL, or is not locatable via HTTP.






                                                    share|improve this answer















                                                    Here's a simple rule:



                                                    PUT to a URL should be used to update or create the resource that can be located at that URL.



                                                    POST to a URL should be used to update or create a resource which is located at some other ("subordinate") URL, or is not locatable via HTTP.







                                                    share|improve this answer














                                                    share|improve this answer



                                                    share|improve this answer








                                                    edited Sep 16 '17 at 20:24









                                                    Peter Mortensen

                                                    13.7k1986111




                                                    13.7k1986111










                                                    answered Dec 11 '13 at 22:15









                                                    Adam GriffithsAdam Griffiths

                                                    1,17198




                                                    1,17198








                                                    • 1





                                                      PUT is not for update, it is for replace, note that to create you are replacing nothing with something. POST is absolutely not for update in any shape of form.

                                                      – thecoshman
                                                      Jun 8 '15 at 8:10






                                                    • 1





                                                      Does the http spec say that? Or are you basing your comment on something else?

                                                      – Adam Griffiths
                                                      Jul 10 '16 at 20:41











                                                    • It's just common sense, how you update something when you don't know what it is you are updating? POST is for creating a new resource.

                                                      – thecoshman
                                                      Jul 27 '16 at 9:23






                                                    • 2





                                                      thecoshman -- you are abusing semantics here -- a replace can be an update if it is the same resource with a few differences. A replace is only valid for put if replace is used to change the same resource. Replacing with a new and different resource is invalid (remove old and add new?), especially if the 'new' resource doesn't have a natural ID. POST, OTOH, is something that can create, update, replace, and delete -- using post depends on whether or not there is a message to interpret, such as 'apply the discount', which may or may not change the resource depending on logic.

                                                      – Gerard ONeill
                                                      Dec 28 '16 at 16:54











                                                    • As for your second comment -- how about you 'get' the resource, modify the fields you need to, and then put it back? Or how about if the resource comes from a different source but uses a natural ID (the external ID) -- put would naturally update the resource at the URL when the original data changed.

                                                      – Gerard ONeill
                                                      Dec 28 '16 at 16:56














                                                    • 1





                                                      PUT is not for update, it is for replace, note that to create you are replacing nothing with something. POST is absolutely not for update in any shape of form.

                                                      – thecoshman
                                                      Jun 8 '15 at 8:10






                                                    • 1





                                                      Does the http spec say that? Or are you basing your comment on something else?

                                                      – Adam Griffiths
                                                      Jul 10 '16 at 20:41











                                                    • It's just common sense, how you update something when you don't know what it is you are updating? POST is for creating a new resource.

                                                      – thecoshman
                                                      Jul 27 '16 at 9:23






                                                    • 2





                                                      thecoshman -- you are abusing semantics here -- a replace can be an update if it is the same resource with a few differences. A replace is only valid for put if replace is used to change the same resource. Replacing with a new and different resource is invalid (remove old and add new?), especially if the 'new' resource doesn't have a natural ID. POST, OTOH, is something that can create, update, replace, and delete -- using post depends on whether or not there is a message to interpret, such as 'apply the discount', which may or may not change the resource depending on logic.

                                                      – Gerard ONeill
                                                      Dec 28 '16 at 16:54











                                                    • As for your second comment -- how about you 'get' the resource, modify the fields you need to, and then put it back? Or how about if the resource comes from a different source but uses a natural ID (the external ID) -- put would naturally update the resource at the URL when the original data changed.

                                                      – Gerard ONeill
                                                      Dec 28 '16 at 16:56








                                                    1




                                                    1





                                                    PUT is not for update, it is for replace, note that to create you are replacing nothing with something. POST is absolutely not for update in any shape of form.

                                                    – thecoshman
                                                    Jun 8 '15 at 8:10





                                                    PUT is not for update, it is for replace, note that to create you are replacing nothing with something. POST is absolutely not for update in any shape of form.

                                                    – thecoshman
                                                    Jun 8 '15 at 8:10




                                                    1




                                                    1





                                                    Does the http spec say that? Or are you basing your comment on something else?

                                                    – Adam Griffiths
                                                    Jul 10 '16 at 20:41





                                                    Does the http spec say that? Or are you basing your comment on something else?

                                                    – Adam Griffiths
                                                    Jul 10 '16 at 20:41













                                                    It's just common sense, how you update something when you don't know what it is you are updating? POST is for creating a new resource.

                                                    – thecoshman
                                                    Jul 27 '16 at 9:23





                                                    It's just common sense, how you update something when you don't know what it is you are updating? POST is for creating a new resource.

                                                    – thecoshman
                                                    Jul 27 '16 at 9:23




                                                    2




                                                    2





                                                    thecoshman -- you are abusing semantics here -- a replace can be an update if it is the same resource with a few differences. A replace is only valid for put if replace is used to change the same resource. Replacing with a new and different resource is invalid (remove old and add new?), especially if the 'new' resource doesn't have a natural ID. POST, OTOH, is something that can create, update, replace, and delete -- using post depends on whether or not there is a message to interpret, such as 'apply the discount', which may or may not change the resource depending on logic.

                                                    – Gerard ONeill
                                                    Dec 28 '16 at 16:54





                                                    thecoshman -- you are abusing semantics here -- a replace can be an update if it is the same resource with a few differences. A replace is only valid for put if replace is used to change the same resource. Replacing with a new and different resource is invalid (remove old and add new?), especially if the 'new' resource doesn't have a natural ID. POST, OTOH, is something that can create, update, replace, and delete -- using post depends on whether or not there is a message to interpret, such as 'apply the discount', which may or may not change the resource depending on logic.

                                                    – Gerard ONeill
                                                    Dec 28 '16 at 16:54













                                                    As for your second comment -- how about you 'get' the resource, modify the fields you need to, and then put it back? Or how about if the resource comes from a different source but uses a natural ID (the external ID) -- put would naturally update the resource at the URL when the original data changed.

                                                    – Gerard ONeill
                                                    Dec 28 '16 at 16:56





                                                    As for your second comment -- how about you 'get' the resource, modify the fields you need to, and then put it back? Or how about if the resource comes from a different source but uses a natural ID (the external ID) -- put would naturally update the resource at the URL when the original data changed.

                                                    – Gerard ONeill
                                                    Dec 28 '16 at 16:56











                                                    5














                                                    In practice, POST works well for creating resources. The URL of the newly created resource should be returned in the Location response header. PUT should be used for updating a resource completely. Please understand that these are the best practices when designing a RESTful API. HTTP specification as such does not restrict using PUT/POST with a few restrictions for creating/updating resources. Take a look at http://techoctave.com/c7/posts/71-twitter-rest-api-dissected that summarizes the best practices.






                                                    share|improve this answer


























                                                    • For the most part, from reading through all this noise, you seem on the ball. I would say though, we should refer to PUT as the replace method, rather than the create/update. I think it better describes in one what it does.

                                                      – thecoshman
                                                      Jun 8 '15 at 8:15
















                                                    5














                                                    In practice, POST works well for creating resources. The URL of the newly created resource should be returned in the Location response header. PUT should be used for updating a resource completely. Please understand that these are the best practices when designing a RESTful API. HTTP specification as such does not restrict using PUT/POST with a few restrictions for creating/updating resources. Take a look at http://techoctave.com/c7/posts/71-twitter-rest-api-dissected that summarizes the best practices.






                                                    share|improve this answer


























                                                    • For the most part, from reading through all this noise, you seem on the ball. I would say though, we should refer to PUT as the replace method, rather than the create/update. I think it better describes in one what it does.

                                                      – thecoshman
                                                      Jun 8 '15 at 8:15














                                                    5












                                                    5








                                                    5







                                                    In practice, POST works well for creating resources. The URL of the newly created resource should be returned in the Location response header. PUT should be used for updating a resource completely. Please understand that these are the best practices when designing a RESTful API. HTTP specification as such does not restrict using PUT/POST with a few restrictions for creating/updating resources. Take a look at http://techoctave.com/c7/posts/71-twitter-rest-api-dissected that summarizes the best practices.






                                                    share|improve this answer















                                                    In practice, POST works well for creating resources. The URL of the newly created resource should be returned in the Location response header. PUT should be used for updating a resource completely. Please understand that these are the best practices when designing a RESTful API. HTTP specification as such does not restrict using PUT/POST with a few restrictions for creating/updating resources. Take a look at http://techoctave.com/c7/posts/71-twitter-rest-api-dissected that summarizes the best practices.







                                                    share|improve this answer














                                                    share|improve this answer



                                                    share|improve this answer








                                                    edited Oct 13 '14 at 9:59

























                                                    answered Oct 6 '14 at 6:51









                                                    java_geekjava_geek

                                                    6,994257295




                                                    6,994257295













                                                    • For the most part, from reading through all this noise, you seem on the ball. I would say though, we should refer to PUT as the replace method, rather than the create/update. I think it better describes in one what it does.

                                                      – thecoshman
                                                      Jun 8 '15 at 8:15



















                                                    • For the most part, from reading through all this noise, you seem on the ball. I would say though, we should refer to PUT as the replace method, rather than the create/update. I think it better describes in one what it does.

                                                      – thecoshman
                                                      Jun 8 '15 at 8:15

















                                                    For the most part, from reading through all this noise, you seem on the ball. I would say though, we should refer to PUT as the replace method, rather than the create/update. I think it better describes in one what it does.

                                                    – thecoshman
                                                    Jun 8 '15 at 8:15





                                                    For the most part, from reading through all this noise, you seem on the ball. I would say though, we should refer to PUT as the replace method, rather than the create/update. I think it better describes in one what it does.

                                                    – thecoshman
                                                    Jun 8 '15 at 8:15










                                                    1 2
                                                    next




                                                    protected by Bala R May 25 '11 at 3:44



                                                    Thank you for your interest in this question.
                                                    Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                                    Would you like to answer one of these unanswered questions instead?



                                                    Popular posts from this blog

                                                    List item for chat from Array inside array React Native

                                                    Thiostrepton

                                                    Caerphilly