share
Stack OverflowWhat is the difference between POST and PUT in HTTP?
[+6450] [42] alex
[2009-03-10 14:25:20]
[ http rest post put ]
[ https://stackoverflow.com/questions/630453/what-is-the-difference-between-post-and-put-in-http ]

Background Information Analysis:

According to RFC 2616, § 9.5 [1], POST is used to create a resource:

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.

According to RFC 2616, § 9.6 [2], PUT is used to create or replace a resource:

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

My Question:

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

(67) 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/… - M Nottingham
(20) Just to bring @MarkNottingham's comment to the latest revision, here's POST and PUT, as defined on HTTPbis. - Marius Butuc
(49) 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
(7) Unfortunally the first answers are wrong about POST. Check my answer for a better explanation of the differences: stackoverflow.com/a/18243587/2458234 - 7hi4g0
(36) 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
(5) you know... I realise the spec refers to PUT as 'update', but I think everyone would be a lot less confused if we said it 'replaced', that is what it does after all. - thecoshman
(3) In pratice, 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 techoctave.com/c7/posts/71-twitter-rest-api-dissected that summarizes the best practices. - java_geek
(4) idempotency is the key. Read PUT or POST: The REST of the Story by John Calcote. If your method is idempotent, go with PUT. If not go with POST. - LCJ
(2) I don't understand the prevailing wisdom on this. OP's citation for PUT begins with "The PUT method requests that the enclosed entity be stored...." That screams "creation" to me. When we talk about "putting" something somewhere, we're talking about a place it hadn't been previously. You don't "put" something to change it. When you amend a document, you don't "put" a new one. The use of the HTTP verb PUT to mean "update" is an ill semantic fit. - Keith Tyler
(1) PUT began as a way for early Microsoft HTML design tools to publish content directly to a server. The fact that it was also used to update (wholesale) was due to the lack of another updating method. Even still since it was a wholesale update, it really was creation, just one that was idempotent. An "update" implies that some aspect of the previous state was maintained. - Keith Tyler
(1) Real world scenario in elastic documentation: elastic.co/guide/en/elasticsearch/reference/current/…. Have a look at difference between all PUT requests and last POST request example. - DevDio
[+4970] [2009-03-10 14:29:46] Brian R. Bondy [ACCEPTED]

Overall:

Both PUT and POST can be used for creating.

You have to ask, "what are you performing the action upon?", 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 you use is 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 the URL objects you create explicitly, or let the server decide? If you name them then use PUT. If you let the server decide then use POST.
  • PUT is defined to assume idempotency, so if you PUT an object twice, it should have no additional effect. This is a nice property, so I would use PUT when possible. Just make sure that the PUT-idempotency actually is implemented correctly in the server.
  • You can update or create a resource with PUT with the same object URL
  • With POST you can have 2 requests coming in at the same time making modifications to a URL, and they may update different parts of the object.

An example:

I wrote the following as part of another answer on SO regarding this [1]:

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/

Additionally, and a bit more concisely, RFC 7231 Section 4.3.4 PUT [2] states (emphasis added),

4.3.4. PUT

The PUT method requests that the state of the target resource be created or replaced with the state defined by the representation enclosed in the request message payload.

[1] https://stackoverflow.com/questions/256349/what-are-the-best-common-restful-url-verbs-and-actions/256359#256359
[2] https://www.rfc-editor.org/rfc/rfc7231#section-4.3.4

(1246) 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
(91) @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
(2) I think there is no difference in definition. 409 Conflict could be a result even for the first request the client made. In any case it should investigate the problem. - Mitar
(735) 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
There are some ideas about how to make POST more reliable on Paul Prescod's website, though the site overall doesn't seem to be kept up to date. - hotshot309
(6) To make things even clearer it might be an idea to add that this example should return an error: PUT /questions HTTP/1.1 Host: wahteverblahblah.com - Martin Brown
(2) I second that, @MartinBrown... your point is quite illuminating for me, actually. The answer as it stands is already fantastic, though, don't get me wrong! - Eric Fuller
(185) @JörgWMittag Idempotence catchphrase? How about "Send and send and send my friend, it makes no difference in the end." - James Beninger
(10) PUT should only be used to create a resource if the client cares about the resource's name. A client caring about a resource's name indicates coupling. Coupling is the result of poor practices. Therefore, use POST. If there is concern about a resource being created twice with POST (since it's not idempotent) read this answer. - Joshcodes
@JörgWMittag If a PUT is idempotent, what happens if the PUT is creating a new resource and the server is the one generating the object id? That would imply every PUT call would create a different object id. Does that mean that a PUT should not/cannot be used in that case, and a POST is required instead? - Eric B.
(2) @EricB. The "object ID" is what he is referring to as the name. Thus, it should be supplied as part of the URL when using POST and not be generated by the server. - Legogris
(1) In pratice, 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 - java_geek
(1) One note on using POST for CRUD style updates: I know that Roy Fielding pointed out that it should be more important for a RESTful design to make use of Hypermedia and discoverable self-explaining APIs than (e.g.) disallowing POST requests to perform item updates. But the quoted http spec gives a rule of thumb: In general it is quite intuitive and consistent to allow POST only for creating new subordinates for the given resource. For replacing or updating existing resources, I always prefer PUT or PATCH. - matthias
Whatever the semantic value of POST vs PUT: If you have to support IE9 and IE8, beware that CORS requests in these versions do not support PUT - only GET/POST. See blogs.msdn.com/b/ieinternals/archive/2010/05/13/… - flexponsive
(3) Can someone elaborate on this sentence. "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." Please explain why this applies to POST and not to PUT? - Kevin Wheeler
(1) @KevinWheeler: Bill de Hora has a good blog post about using POST to update only some fields of a resource, while PUT must supply all fields (whether creating or updating): dehora.net/journal/2009/02/03/just-use-post He makes the point that PUT can be very heavy weight if you have to send through all the fields when you only want to update one. So, following his scheme, you could have POST sending through an update to a couple of fields and another POST request updating another couple of fields in the same resource. Not sure but I think PATCH now does the same sort of thing. - Simon Elms
(72) 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
"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." .......... you mean POST can update? - Raúl
"Used to modify and update a resource" ....... you must meant 'or' here - right ? - Raúl
(1) @Learner I second your sentiment. This answer and the examples are confusing. Words are used inconsistently. - lucid_dreamer
(2) Think like a server: Expect an item to be uniquely identifiable. If I receive a PUT without an identifier I'm going to reject it because it's not telling me what to update. If I receive a POST with an identifier I'm going to reject it because I can't create what already exists. - Brian Lowe
(1) Assuming I having the following URL: test.awesome.com/questions, is the above saying that I would PUT to that URL to create a new resource? I thought it's POST to a collection and PUT to a URL with a specified ID. Isn't it PUT here: test.awesome.com/questions/1 and POST here: test.awesome.com/questions? Or, is it saying if test.awesome.com/questions/1 doesn't exist, PUT will create it or replace it if it already exists on the server? - cr1pto
Does idempotency of PUT mean we cannot have the same resource name or that objects that have the same hashes cannot exist at the same time ? - monolith
@wedran idempotency means, that if someone calls PUT once or multiple times, the outcome is still the same. So an operation, where every call causes a change (e.g. a counter to increment) wouldn't be idempotent ( restapitutorial.com/lessons/idempotency.html ). I would recommend reading through all answers answers here to get a better understanding. It doesn't have anything to do with the resource name or a hash. - skofgar
Am I mistaken, or does this answer contradict the info in the question? There it says Post=Create. Put=Update if possible. If not, create. However here, it says 'Post=Update'. I'm confused. - Mark A. Donohoe
(2) You should really be using PATCH to update a entity, PUT to insert a entity , DELETE to delete/remove a entity and a POST to do other things, like submitting form data type requests. - SynackSA
Would be nice if we could add the difference with PATCH. Right now I'm in a situation where I define the resource's ID beforehand (not the server), I'm therefore using PUT to create it, and then POST to update it, but I could also use PATCH instead of POST and that would make more sense, semantically speaking. - Vadorequest
(1) @BrianR.Bondy you Wrote: POST: Used to modify and update a resource POST /questions/<existing_question> HTTP/1.1. But in this case you make POST method idempotent. This is in contrast with HTTP specification! RFC 7231 - Glauco Cucchiar
@BrianR.Bondy in the below link definitions are contradictory restfulapi.net/rest-put-vs-post - user7030548
@Sirius - I thought the same thing, but technically POST can be used to append to an existing resource as stated in Section 4.3.3 of RFC 7231 - 4th bullet point. I would consider appending as an update but it could be interpreted differently - commandt
"You do not need to support both PUT and POST," is followed by, "If the URL is not yet created, you should not be using POST to create it while specifying the name... You should PUT the <new_question> resource on the server first." Perhaps, "You do not need to support POST if you support PUT"? Or is @SynackSA on to something? - ruffin
does idempotent mean that if a ressource already exist and is similar the PUT method preserve some ressource by overcome the point and go directly to the next point ? A kind of optimize save in other word - Diagathe Josué
(2) Isn't author's claim that POST can be used to modify/update incorrect? For example he is using POST /questions/<existing_question> which means that the POST request is aware of the location of the resource which is <existing_question> but this means POST is showing idempotent behavior cause no matter how many time we make this request the same resource(<existing_question>) will be accessed at the server which is not correct as POST is supposed to be non-idempotent which means every time we invoke this it should change the state of the server. - YK S
@EugenKonkov - So what happens when I use post to update an existing resource as mentioned in the answer ? Do we get back a new resource or do the updates get applied to the existing resource ? - MasterJoe
@JörgWMittag - Please see the comment by Eugen Konkov. Does that sound correct ? Here is the comment - 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. - MasterJoe
(1) @MasterJoe: indepondent means that despite on any requests you made you will get same result. For Our PUT. When you did one request - you get one record in database with your dara. When you did two requests - you get one record in database with your data. When you did N requests you get one record in database with your data. When you do POST request - after each request you will get new additional record in database with your (POSTed) data. - Eugen Konkov
(1) @MasterJoe: No body does not restrict you to make POST requests to your site indepondent. This is up to your site API. This probably will be just a surprise for your users who does not read your API's documentation. - Eugen Konkov
We send data through both. And do whatever we want with both - Ali.Rashidi
Didn't get the phrase ``` 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. ``` How could post be used for modifications ? - gstackoverflow
Is this 5000 upvoted selected answer actually wrong? This being one of the most important questions on Stack we should really fix this. The answer says that post CANNOT create a resource, where its actual main point is to create? Reading closer I think the main point should be post to a url shouldn't be a new url, only existing. Example post /questions with a body question OK. post /questions/external-identifier-for-question is an issue? - Morgeth888
1
[+2646] [2010-04-22 14:55:54] Cheeso

You can find assertions on the web that say

Neither is quite right.


Better is to choose between PUT and POST based on idempotence [3] 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
[1] https://web.archive.org/web/20100819115115/http://www.oracle.com/technetwork/articles/javase/index-137171.html
[2] https://awsmedia.s3.amazonaws.com/pdf/RESTandS3.pdf
[3] https://en.wikipedia.org/wiki/Idempotence

(89) I agree, wherever idempotence is concerned it should trump any other concerns since getting that wrong can cause many many unexpected bugs. - Josh
(2) Yes. In fact, AtomPub protocol defies this (or more accurately, restricts its semantic meaning of PUT): "PUT is used to edit a known Resource. It is not used for Resource creation." Just because AtomPub protocol says so (which is valid by the way) doesn't mean all RESTful protocols must follow it. (because REST is generic) - Hendy Irawan
(22) 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. - Jack Ukleja
(34) @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
(57) @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
(3) This is dead on, and the "accepted" answers look to me (@brian) to miss this critical subtlety. - Andrew Roberts
(15) 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)? - jrz
This is dead wrong. Idempotence should not decide if PUT or POST is used. Who owns the responsibility for the URL structure decides if PUT or POST is used. To understand how to handle duplicate POST requests see this answer. - Joshcodes
(1) I wonder if what he means by "POST can update a resource" is that by POSTing to a collection you are adding an item to it and therefore "updating the collection". That distinction seems a bit misleading. I like the answer, though. - Thomas
(2) Thank you, this is very helpful. To further your analogy, POST may be used to "assign" (as opposed to x++), but it's more like obj.setX(5) -- the setX method should result in x being 5, but may have side effects. - Nicole
If using POST to partially update an existing resource, such as updating only a couple of fields, can you specify the resource name? The reason I ask is that many people say POST can't be used with an individual resource, only with its parent collection. eg POST applied to expense reports. Is it possible to update say a single field of expense report 10929 using POST /expense-report/10929 ? - Simon Elms
(4) Comparing PUT and POST to mysql: POST is like an INSERT INTO without providing the ID of an auto-increment column, if you do it multiple times, you will create multiple rows. PUT is like an UPDATE using the primary key in the WHERE statement, if you do it multiple times, the row should be the same after each one. Some vendors may also allow you to PUT to create, which would be like an INSERT INTO providing a value for the auto-increment column. - Jo.
Helpful mnemonic: IdemPUTent - Mercury
@jackUkleja, I don't think Cheeso is saying a POST CANNOT be idempotent. But, rather that PUT MUST be idempotent while a POST doesn't have to be. The spec for PUT talks more about idempotency. The input to a PUT must be the document itself (e.g., the user or whatever your resource is) while the input to a POST can be parameters indicating how to perform an operation. E.g., you could pass the userId and age to a POST (though a PATCH would be better now) whereas you must pass the whole user for a PUT. - stuckj
2
[+866] [2010-04-07 05:52:52] Nigel Thorne
  • 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. [1]

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. [2]

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

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 [4]

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.

[1] http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.5
[2] https://www.rfc-editor.org/rfc/rfc5789
[3] https://www.thoughtworks.com/radar/techniques/rest-without-put
[4] https://www.thoughtworks.com/insights/blog/rest-api-design-resource-modeling

(71) 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
(4) 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
(4) PATCH isn't a realistic option for at least a few years, but I agree with the ideology. - crush
(5) 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
(5) 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
(3) @crush: Why isn't PATCH a realistic option for at least a few years? - arthropod
@NigelThorne The only answer that talks about server defined and client defined URLs. Who does validation on client defined URLs? - realPK
(1) @PK_ the server validates the URL. The client would have to adhere to the url schema the server supports. Invalid urls would be rejected by the server. - Nigel Thorne
I'm working on a project where the id's are known to the client. So I'd like to use PUT for creation. However, a colleague disagrees with the argument that it would be confusing if the resource would get deleted while someone else does a PUT (to update as a whole), now the resource would be recreated, even though it was deleted. His point is, that in a POST, PUT, DELETE scenario PUT should return a 404 if someone wants to update a resource and it doesn't exist. Any thoughts that might help? - skofgar
(2) HTTP says ... when a client 'puts' to a url, it is requesting that url to hold that data. It's up to the server to decide if it's happy to do that. If you want 'DELETE' to stop PUTs working... then that's fine, but can you ever have anything at that url ever again? If so, then you should probably let the subsequent PUT through... after all one of your users is trying to put content at that location for a reason. Who's to say who was more correct? :) the answer is.. "your domain". Do what models your domain. but PUT means "I am trying to store this here". - Nigel Thorne
(1) @skofgar In your scenario (clients know the ids and drive the creation). What about PUT to create and PATCH to partially update? You save some bandwidth and PATCH could fail if the resource is deleted. - Marcel Toth
Good idea, I haven't seen PATCH being used very widely, furthermore I want to keep the API's as simple as possible. But I think using PATCH would have made sense in this case. - skofgar
(1) I was going to use PUT to edit profile but after reading the article and talking with the client, he told he only wanted users to edit specific parts of profile and I consider that to be a perfect application for /changeAddress of something as mentioned in the blog post as it puts the server back in control. And removes the state from the client. Not to mention that I can now record stuff like how often do users edit their profile and what do they change - Hemil
3
[+340] [2011-10-23 14:27:31] Alexander Torstling

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.


(31) 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
(2) 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
@Coulton: I meant what I wrote. You insert user 5 if you PUT to /users/5 and #5 does not exist yet. - Alexander Torstling
@Coulton: And PUT can also be used to replace the value of an existing resource in its entirety. - DavidRR
(2) "Prefer PUT over POST"... care to justify that? - thecoshman
(2) @thecoshman: Sure. I wrote that as a general advise. My reasoning is that PUT is idempotent, hence better from a network perspective. POST is also more general, so by recommending PUT you avoid POST being used for situations where PUT would have sufficed. POST is also heavily overused due to browser restrictions, and so a recommendation against it will have positive effects for REST as a concept. There are also some positive effects in the URL scheme when the clients are in control of the URL construction IMO, but I cannot fit that into a comment here. - Alexander Torstling
@AlexanderTorstling excuse whilst I try to tease out solid reasoning others might benefit from. Better from a network perspective how? Sure if your network goes down and you failed to confirmation you can just re-create, but what if it did work, and then someone else modified? Your re-PUT will overwrite what they did. You make it sound like POST has some extra overhead compared to PUT (where PUT would have sufficed). I think the fact POST is overused should not mean we advise against it when it is the correct choice, just learn to advise the right choices. That Last point is rather subjective. - thecoshman
@thecoshman: I was thinking of retries, yes. PUT concurrency can be controlled by conditional headers like If-Modified et al. Doing the same for POST would result in much coarser locking. You have a point in advocating the right choices, I however had no intention of banning POST. Perhaps "Use PUT when possible" would have been a better wording? - Alexander Torstling
@AlexanderTorstling well, my main point was that your answer states you should have a preference, with no justification for it. Your responses here however seem fairly justifiable. That said, I don't think it's the correct reasoning to use for PUT vs POST - thecoshman
Here is one benefit that using POST to create a resource has over PUT. In this case, if you try to modify a resource that doesn't exist yet (using PUT), you will get an error. To generalize this advice beyond PUT vs POST, try to not to use an interface where the result/action depends on the current state, because the user may have an incorrect assumption about what the current state is (in the put example, he thinks the resource already exists). Use two separate interfaces and throw an error if the current state does not match the preconditions. - Kevin Wheeler
PS I still don't understand what is meant by "Better from a network perspective." I can, however, think of at least one reason on my own as to why PUT would be better from a network perspective: the results can be cached (perhaps by a proxy cache server). - Kevin Wheeler
(2) I would say that POSTing twice with the same data MAY result in two identical users. Were I creating my API, if someone tried to POST a new user with the same email address, but different data, I might issue a 409. If someone tried to POST a new user with identical data, I might issue a 303. I probably wouldn't want my system to be able to have two identical users. - Dan Jones
4
[+292] [2013-08-14 22:47:18] 7hi4g0

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.example/users/john
website.example  - whole site
users        - collection of users
john         - item of the collection, or a resource

URI:website.example/users/john/posts/23
website.example  - 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:

[1] https://www.rfc-editor.org/rfc/rfc7231#section-4.3.3
[2] http://en.wikipedia.org/wiki/Representational_state_transfer
[3] http://www.ietf.org/rfc/rfc2396.txt

(19) 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
It is a very reasonable way to implement a CRUD API that is REST/http complient. A good reading: the RFC about HTTP, and in particular what is idempotent and the expected behavior of web cache. The use of POST/PUT are constrained by the expected behavior of cache (web or user agent) - mcoolive
(5) 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
@thecoshman I have posted in my answer the relevant parts of the HTTP/1.1 spec. Read the last part of my answer entitled Spec. The spec clearly states that PUT is used to create or update an entity. - 7hi4g0
(4) @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
@thecoshman I never said that PUT should be used for partial modification. If you don't like the terminology, I can restate. The spec clearly states that PUT is used to create or modify an entity. - 7hi4g0
@thecoshman I guess you can say, create or replace, but the two are different actions according to the Spec. Either way, update, modify, replace are only terms. - 7hi4g0
@7hi4g0 my point is, if you read the rest of that sentence from the spec, you are updating that URI to have a completely new resource at that location. As such, it is replace 100% with what data is included in this PUT request. Likewise, I'd argue that create is effectively just replacing 'nothing with something'. And so, you can "Keep It Simple Stupid" by just saying "PUT is for replacing". - thecoshman
(2) @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
@7hi4g0 I would generally say that POST is (usually) more appropriate for create, but still. I think we can leave it at that, we've at least given people sensible discussion to think about, which IMO is the most important thing to have when you have fluffy 'guideline' :P - thecoshman
PUT can be used to create a resource and is appropriate for creating resources if the client is responsible for providing the URL structure. However, it is highly unlikely that in your solution it is appropriate for the client to provide the URL structure. - Joshcodes
(1) Of course you can POST an 'update'. If you keep prior versions around (and there are many reasons why you may want to do so) then your update is not idempotent and so cannot be expressed by PUT. (Or in other words everything turns into a collection when you stare at it hard enough) - CurtainDog
upvoted! curious question when you do insert on conflict do nothing, i am guessing post is what should be used since put would actually replace it - PirateApp
@thecoshman I have been studying this issue on this page and your 2nd comment here eventually helped me understand the difference. Thank you. - okaeiz
5
[+194] [2011-01-15 19:59:06] ThaDon

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

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

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

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

(20) 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
(6) @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
(4) @thecoshman You can read it like 'from users get me id 5' ;) - xuiqzy
(4) @xuiqzy hmm, I quite like this way of thinking about it really, and expands nicely GET /users/5/documents/4/title would be like 'get the users, from there get me user 5, from there get me the documents, from there get me document 4, from there get me the title' - thecoshman
6
[+174] [2015-09-11 13:12:44] Premraj

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

PUT POST
Replacing existing resource or creating if resource is not exist. www.example.com/com/customer/{customerId} www.example.com/com/customer/123/order/{orderId} Identifier is chosen by the client. Creating new resources and subordinate resources, e.g. a file is subordinate to a directory containing it or a row is subordinate to a database table. www.example.com/com/customer/ www.example.com/com/customer/123/order/ identifier is returned by server
Idempotent i.e. if you PUT a resource twice, it has no effect. Example: Do it as many times as you want, the result will be same. x=1; POST is neither safe nor idempotent. Example: x++;
Works as specific Works as abstractive
If you create or update a resource using PUT and then make that same call again, the resource is still there and still has the same state as it did with the first call. Making two identical POST requests will most likely result in two resources containing the same information.

Analogy:

  • PUT i.e. take and put where it was.
  • POST as send mail in post office.

enter image description here

Social Media/Network Analogy:

  • Post on social media: when we post message, it creates new post.
  • Put(i.e. edit) for the message we already Posted.

(24) @MobileMon No, REST methods are not CRUD. - jlr
(4) I'd say PUT for UPSERTS - Hola Soy Edu Feliz Navidad
(1) @MobileMon no : POST when you create a new resource and you don't know the final endpoint to get it. PUT for other cases. - Portekoi
7
[+158] [2009-03-10 14:28:57] Tim Sullivan

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

(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.
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.
It has 12 votes because it predates the change that was made that added the group thing. I've reverted the change. - Tim Sullivan
(9) 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
(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
@BradKoch You are right with the statement; but when you are designing a RESTful API, you need to ensure that its consistent so that it makes life easy for clients consuming your API's. If you are using POST/PUT interchangeably to create/update resources it makes life hard for your consumer. You would want to keep it easy for your clients because thats how you grow your consumer base. - java_geek
(3) 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
Nice and short +1. For extra details one can read further :) - Akif
8
[+75] [2009-03-10 15:27:52] Jörg W Mittag

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) [1] 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.

[1] https://www.ietf.org/rfc/rfc5023.txt

(9) There's nothing wrong with allowing PUT to create resources. Just be aware that it means that the client provides the URL. - Julian Reschke
(6) There's something very wrong with allowing PUT to create resources: the client provides the URL. That's the server's job! - Joshcodes
@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
@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
@Joshcodes It's matter of separating concerns. Where the URL is generated is actually of little consequence. Yes the server is responsible for delivering content from the correct URL but that does not limit a server from responding to a request on an incorrect URL. The correct response from the server in this case is a 308. A proper client will then retry the PUT on the correct URL. Another example is a distributed system where not all nodes know of all resource provided by clients. Here a PUT to create would be perfectly valid because for that server node the resource does not exist. - Justin Ohms
@JustinOhms I agree completely it's about separating concerns. I also agree there are edge cases for using PUT (one encountered recently by me involved a data import than needed the ability to re-run). The approach you have outlined above works because the server "instruct(s) clients on how to construct appropriate URIs", (Roy Fielding's words). However, it is not obvious to me why that is better than just using POST as intended. - Joshcodes
@Joshcodes The reason to use a PUT like this in a multi node setup is that that for the client to switch from PUT to POST the client would require knowledge about the state of the data on that node. It seems odd because we are often think of the server as being a single authority however in a distributed system with eventual consistency the client may actually have more information than any given server node at any point in time. In this case the client knows the resource exists, the node does not. (maybe a sync is needed) Plus POST would not be appropriate because the resource does exist. - Justin Ohms
(2) 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-driv‌​en - Joshcodes
Any reference for "with some input from Roy Fielding"? - wlnirvana
9
[+72] [2013-10-29 23:00:55] Joshcodes

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 [1] 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 [2] is designed to replace 2616 and in Section 4.3.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 [4].

[1] http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.10
[2] https://www.rfc-editor.org/rfc/rfc7231
[3] https://www.rfc-editor.org/rfc/rfc7231#section-4.3.3
[4] http://articles.joshcodes.com/?p=5

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.
@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
@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
@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
@Joshcodes i'm seeking an answer to stackoverflow.com/questions/39057416/…. commenting now to point your attention to it. - Roam
@Joshcodes i'm aware the work on stackoverflow.com/questions/39057416/… doesn't comply w/Restful architecture. yet works for our case as an exception. - Roam
10
[+57] [2012-01-11 17:18:54] metamatt

I like this advice, from RFC 2616's definition of PUT [1]:

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.
[1] http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

(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
11
[+57] [2016-07-29 11:11:21] bharatj

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.


(7) for PUT is similar to INSERT or UPDATE query - Eugen Konkov
(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
12
[+53] [2013-06-14 18:10:25] Homer6

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


(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
(6) The id is part of the URL, so yes, use PUT if you know the URL (which includes the id). - Homer6
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
@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
(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
@Joshcodes, I'm not denying where a URL is determined. I'm saying that in a RESTful architecture (defined from the server on up), the ID is part of a URL. - Beez
@Beez if the server determines the URL, the URL should not be dictated by the client. Therefore, even if you know the ID, use POST not PUT. Whether the ID should be part of the URL is a different conversation. - Joshcodes
I think "General Rule" covers it generally having the ID or name in the URL. Great answer! - TamusJRoyce
@TamusJRoyce, Roy Fielding (the guy who created REST) would disagree with you: roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driv‌​en Also, while true that the ID will likely be provide by the client via the URL the client still does not know where in the URL to put the ID. - Joshcodes
13
[+50] [2017-10-15 02:33:32] ishandutta2007

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.


(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
14
[+48] [2012-08-01 20:10:42] Jordan

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 [1]): "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 [2] 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."

[1] http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm#sec_5_2_1_1
[2] https://stackoverflow.com/a/9194942/972499

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
(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
@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
(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
15
[+42] [2012-02-26 09:21:30] germanlinux

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 [1]" explains it.

[1] http://weblog.rubyonrails.org/2012/2/26/edge-rails-patch-is-the-new-primary-http-method-for-updates/

16
[+34] [2018-09-25 08:28:16] Rohit Dhiman

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


(4) Why was this not upvoted? If true, this is a critical distinction is it not? - Iofacture
(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
17
[+32] [2011-03-25 10:17:54] skillet-thief

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.


(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
(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
(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
@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
@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
@thecoshman yes, the client really should never use PUT. What's RPC about that? - Joshcodes
@thecoshman: no, you can't have multiple URLs pointing to one resource. Every unique URI is a unique resource, and every resource has one and only one URI. - Nicholas Shanks
@NicholasShanks not really. Consider a resource that represents a school student, you could access /student/123 but you might also get to that same student via /class/abc/student/123, That is perfectly valid. There is the concept of the canonical URI though, which in this case we could say the former is. The U is for Uniform, not Unique. - thecoshman
@thecoshman What you would then have is two identical resources that just happen to change every time the other one does. A link from one to the other with a canonical relation would definitely help though. Even if you supplied a Content-Location header with one, pointing to the path of the other, that would not unify the underlying resources represented by each URL. Consider that a representation is not a resource, and two identical representations do not make the same resource (e.g. downloads/v1.0.0/ and downloads/latest-version/) - Nicholas Shanks
@NicholasShanks I think this answer provides a good explanation that I agree with but also considers your point of view. I'd also say that it is probably a more relevant place for this discussion. - thecoshman
18
[+30] [2017-02-14 06:55:05] UniCoder

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.


(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? - Mohammed H
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
19
[+28] [2012-02-08 16:31:54] Hans Malherbe

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

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
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
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
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
@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
invalidates cached copies of the entire containing resource any reference for that? Also it's never mandatory for a browser to follow the cache rules set. - mfaani
20
[+21] [2016-02-18 11:45:38] bbsimonbb

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


(2) Wont storing response be like maintaining a session? Which would cause (horizontal) scaling issues. - Saurabh Harwande
21
[+20] [2013-10-10 04:18:51] Burhan

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.


(4) Update => HTTP POST : POST is not for updating - Premraj
@premraj You made the assumption that Burhan is telling you not to make; namely, you are conflating CRUD, REST, and HTTP. If you read RFC 7231, where these things are defined, you will find that in HTTP protocol, the definition of POST certainly allows updating. It is only the constraints of REST that say otherwise. - IAM_AL_X
22
[+18] [2014-01-16 07:58:14] inf3rno

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


(1) I think you've managed to provide one of the few good examples of how to use PUT, well done. - thecoshman
23
[+17] [2013-10-21 21:16:32] Gerard ONeill

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.


24
[+14] [2013-12-11 22:15:48] Adam Griffiths

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.


(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
(3) Does the http spec say that? Or are you basing your comment on something else? - Adam Griffiths
(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
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
25
[+13] [2017-06-21 17:38:44] tothemario

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 ;)


26
[+13] [2020-12-04 20:56:49] Géry Ogam

Summary

  • Use PUT to create or replace the state of the target resource with the state defined by the representation enclosed in the request. That intended effect is standardized and idempotent so it informs intermediaries that they can repeat a request in case of communication failure.
  • Use POST otherwise (including to create or replace the state of a resource other than the target resource). The intended effect is not standardized so intermediaries cannot assume any property.

References

The latest authoritative description of the semantic difference between the POST and PUT request methods is given in RFC 9110 [1] (Roy Fielding, Mark Nottingham, Julian Reschke, 2022):

The fundamental difference between the POST and PUT methods is highlighted by the different intent for the enclosed representation. The target resource in a POST request is intended to handle the enclosed representation according to the resource's own semantics, whereas the enclosed representation in a PUT request is defined as replacing the state of the target resource. Hence, the intent of PUT is idempotent and visible to intermediaries, even though the exact effect is only known by the origin server.

In other words, the intended effect of PUT is standardized (create or replace the state of the target resource with the state defined by the representation enclosed in the request) and so is generic to all target resources, while the intended effect of POST is not standardized and so is specific to each target resource. Thus POST can be used for anything, including for achieving the intended effects of PUT and other request methods (GET, HEAD, DELETE, CONNECT, OPTIONS, and TRACE).

But it is recommended to always use the more specialized request method rather than POST when applicable because it provides more information to intermediaries for automating information retrieval (since GET, HEAD, OPTIONS, and TRACE are defined as safe), handling communication failure (since GET, HEAD, PUT, DELETE, OPTIONS, and TRACE are defined as idempotent), and optimizing cache performance (since GET and HEAD are defined as cacheable), as explained in It Is Okay to Use POST [2] (Roy Fielding, 2009):

POST only becomes an issue when it is used in a situation for which some other method is ideally suited: e.g., retrieval of information that should be a representation of some resource (GET), complete replacement of a representation (PUT), or any of the other standardized methods that tell intermediaries something more valuable than “this may change something.” The other methods are more valuable to intermediaries because they say something about how failures can be automatically handled and how intermediate caches can optimize their behavior. POST does not have those characteristics, but that doesn’t mean we can live without it. POST serves many useful purposes in HTTP, including the general purpose of “this action isn’t worth standardizing.”

[1] https://www.rfc-editor.org/rfc/rfc9110#section-9.3.4-9
[2] https://roy.gbiv.com/untangled/2009/it-is-okay-to-use-post

27
[+13] [2021-09-17 09:51:46] Moritz

To me, the key of understanding the difference was to understand who defines the ID of the resource:

Example (with some address service)

POST (sever creates new resource)

client               server/addresses      // NOTE: no ID in the request
  |                                 |
  | --{POST address data}-->        |
  |                                 |
  | <--{201, created addresses/321} |      // NOTE: resource ID in the reply
  |                                 |
PUT (sever sets data of resource, creating it if necessary)

client               server/addresses/321      // NOTE: *you* put the ID here!
  |                                 |
  | --{PUT address data (to 321)}-->|
  |                                 |
  | <--{201, created }              |          // NOTE: resource ID not required here
  |                                 |

There are many great answers with great details below, but that helped me to get to the point.


Nice and simple. A little too terse.., but hopefully this distinction is placed amongst the other lessons around REST :). - Gerard ONeill
28
[+12] [2011-10-23 20:07:55] Gregory Magarshak

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.


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
29
[+12] [2016-06-29 21:13:07] Rajan

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.


30
[+11] [2014-07-21 20:02:35] Tom Stickel

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.


31
[+8] [2021-11-21 14:11:14] Mahozad

POST is used to send data to a server.
PUT is used to deposit data into a resource on the server (e.g., a file).

I saw this in a footnote (page 55) from the book HTTP: The Definitive Guide [1].

[1] https://www.oreilly.com/library/view/http-the-definitive/1565925092/

32
[+7] [2014-10-06 06:51:56] java_geek

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.


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
33
[+6] [2013-12-12 11:06:52] sushil pandey

POST: Use it for creating new resources. It's like INSERT (SQL statement) with an auto-incremented ID. In the response part it contains a new generated Id.

POST is also used for updating a record.

PUT: Use it for creating a new resource, but here I know the identity key. It's like INSERT (SQL statement) where I know in advance the identity key. In the response part it sends nothing.

PUT is also used for updating a resource


(6) 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
34
[+5] [2023-03-29 04:52:54] Muhammad Ali

Where PUT is Used?

The PUT technique is mostly used to update server-side resources. Usually, it replaces anything that is present at the destination URL with something new. It can be used to create brand-new resources or replace old ones. PUT demands that the enclosing entity be saved under the given requested URI. (Uniform Resource Identifier).

Where POST is used?

A web server mostly receives the data in the message body that is requested using the POST method, which is provided by HTTP. The World Wide Web frequently uses POST when sending user-generated content to a web server or when you upload a file.

There is a lot of difference

Put Method Post Method
Caching: PUT method responses can be cached Caching: you cannot cache POST method responses.
Query: You can use UPDATE query in PUT Query: you can use create query in POST.
Decision made by client: The client decides which URI resource should have Decision made by server: the server decides which URI resource should have.
Specific in Work: PUT works as specific Abstract in work: while POST work as abstract
Type of result: If you send the same PUT request multiple times, the result will remain the same. Type of result: if you send the same POST request multiple times, you will receive different results.
Syntax: PUT /questions/{question-id} Syntax: POST /questions

Both have their own advantages

Put Method Advantages Post Method Advantages
You can keep the given entity at the given URL with its assistance. If the supplied entity already exists, you can either create using that URL or conduct the update action.
You can create a resource as many times as you like. This technique aids in determining the website URL.
Creating a resource with PUT method is very easy. Using location heading makes it very simple to specify a new resource location header.
You don't need to make sure the user hasn't repeatedly pressed the submit option. User-generated content can be sent to the computer server.
The entity that is contained with the request can be identified You can keep the data private.

Use POST or PUT according to your project needs.

Hope it helps.


35
[+4] [2019-06-26 09:01:14] baptx

I think there is also an interesting point that was not shared on this PUT vs POST question:

If you want to have a web application that works without JavaScript (for example if someone is using a command-line browser like Lynx or a browser addon like NoScript or uMatrix), you will have to use POST to send data since HTML forms only support GET and POST HTTP requests.

Basically if you want to use progressive enhancement (https://en.wikipedia.org/wiki/Progressive_enhancement) to make your web application work everywhere, with and without JavaScript, you cannot use other HTTP methods like PUT or DELETE, which were only added in HTTP version 1.1.


36
[+4] [2021-10-11 05:54:05] Michael Chourdakis

All the answers above and below are correct, just a small (important) note. All these "verbs" are recommendations and their effect is not enforced. The server is free to do whatever they want and this means writing with GET or whatever the server wants. It all depends on the implementation backend.

PHP for example, reads $_POST and $_GET. It's entirely up to the programmer what exactly it will be done by reading variables from these arrays.


37
[+3] [2018-05-08 02:39:46] maaartinus

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

You should use PATCH. You PATCH the list of questions like

PATCH /questions HTTP/1.1

with a list containing your to be created object like

[
    {
        "title": "I said semantics!",
        "content": "Is this serious?",
        "answer": "Not really"
    }
]

It's a PATCH request as

  • you modify the existing list of resources without providing the whole new content
  • you change the state of your new question from non-existing to existing without providing all the data (the server will most probably add an id).

A great advantage of this method is that you can create multiple entities using a single request, simply by providing them all in the list.

This is something PUT obviously can't. You could use POST for creating multiple entities as it's the kitchen sink of HTTP and can do basically everything.

A disadvantage is that probably nobody uses PATCH this way. I'm afraid, I just invented it, but I hope, I provided a good argumentation.

You could use CREATE instead, as custom HTTP verbs are allowed, it's just that they mayn't work with some tools.

Concerning semantics, CREATE is IMHO the only right choice, everything else is a square peg in a round hole. Unfortunately, all we have are round holes.


I think you should be careful in stating that PATCH should be used without providing some clarifying statements. PATCH is great for updating resources, but it's not meant for creating resources. The HTTP specs specifically call out that it is only for updating resources. You can make anything work as you've stated, but the verbs lose their meaning when you arbitrarily give them non-standard functionality. - Jitsusama
@Jitsusama Read my last but one sentence.... and maybe google for this "It’s like trying to hack a programming paradigm out of the TCP packet header control bits! If URG is high then my calendar appointment is very important. If ACK is low, then I’m denying your friend request.". - maaartinus
I think this is not a good idea. Basically, if you say inserting into a list is a patch, everything becomes a patch: inserting, updating, even deleting. This does not solve the problem but actually adds another ambiguity. - dariok
@dariok Agreed, it'd make it even a bit worse than it already is. - maaartinus
38
[+3] [2019-09-20 13:31:12] Md. Tazbir Ur Rahman Bhuiyan

Addition to all answers above:


Most commonly used in professional practice,


  • we use PUT over POST in CREATE operation. Why? because many here said also, responses are not cacheable while POST ones are (Require Content-Location and expiration).
  • We use POST over PUT in UPDATE operation. Why? because it invalidates cached copies of the entire containing resource. which is helpful when updating resources.

39
[+2] [2022-05-01 16:56:28] Charles Derek

In the simpliest explained way:

POST does what it says, POST means it's presenting a request for a new object creation. MDN referse to this as 'other side-effects', an example being incrementing indexes (What the word 'POST' implies).

PUT can be thought of as updating existing data objects, When people are saying it can be used for adding items. This is because it can update child null values from an existing parent object.

MDN Method PUT Documentation [1]

[1] https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT

(1) The reason this is all a tad confusing is because the word POST doesn't say or even suggest any clear meaning - certainly nothing about being "NEW". So I'm mystified by your first sentence telling us the word POST does what it says - PandaWood
What trips people up is Putting something can imply Posting (but not the converse). As a noun it doesn't make sense, but only in the verb tense it does. Thinking of when putting the method into an html form, you have to add an Action and Method (both do things and are verbs). The action of placing a post-it on a board, to Post It means you're placing something new there and not editing an existing one. I can relate, even b.i.t.d JavaScript was confusing Juniors with Java because poor word choice when it came out. It's rare when a word in programming is not implicit lol. - Charles Derek
40
[+2] [2023-01-24 19:07:26] Stokely

POST vs PUT

Let's get the facts on POST and PUT according to the Internet Engineering Task Force (IETF):

The target resource in a POST request is intended to handle the enclosed representation according to the resource's own semantics, whereas the enclosed representation in a PUT request is defined as replacing the state of the target resource.

Proper interpretation of a PUT request presumes that the user agent knows which target resource is desired. A service that selects a proper URI on behalf of the client, after receiving a state-changing request, SHOULD be implemented using the POST method rather than PUT.

https://www.rfc-editor.org/rfc/rfc7231#section-4.3.4

Based on the above, POST should be used to do MOST (if not all) of your CREATE or ALTER data routines on the server. Only POST is supported in HTML form field methods for this very reason, while PUT and DELETE are not supported in HTML forms (see below).

PUT acts as a sub-type of the POST method as it can do only one narrow range of actions that POST does. Both POST and PUT can create and change data. The different is "intent". POST has no intention other than to work with the server's various ways of creating or altering the state of data. PUT has one intent...to REPLACE the state of ONE RESOURCE on the server with its own (which POST can also do, btw, and does often online).

Because of heavy use of JavaScript the past decade and XMLHttpRequest calls, this has opened the door up to websites using PUT and DELETE more often. WebAPI's are now configured to apply Http Method PUT and the rules to use it more consistently in 2023, than in the past. So the question has been raised, why use it, when we have been using POST to do the same thing successfully the past 20+ years?

I would ONLY use PUT in one case...where you are using JavaScript to create or alter the identity of a "resource" (state of an item), know the resource identity in your PUT (most often a URI/URL), and the WebAPI endpoint on the server strictly honors change the resource state with the one sent in the PUT. This must be total and complete, unlike with a post.

In the case of PUT, a good way to enforce this, rather than rely on sketchy JavaScript rules, is to rely on a UNIQUE IDENTIFIER in the URL on the server. This prevents PUT coming in without any rules or identifier. An example might be: https://example.com/123, with "123" being the ID of some resource or data point at the WebAPI endpoint.

The URI in a POST request does not need to identify the resource that will handle the enclosed entity or its state or what is changed. In contrast, the URI in a PUT request identifies the entity enclosed with the request and what will be replaced with the data or state in a PUT request.

BROWSERS DO NOT SUPPORT "PUT" or "DELETE"!

Did you know that HTML Form method attributes in browsers the past 20+ years have only supported POST and GET, and do not support PUT?

POST and GET work ok...
<form id="form1" name="form1" method="get" action="/form">...</form>
<form id="form2" name="form2" method="post" action="/form">...</form>

PUT NOT SUPPORTED!
<form id="form3" name="form3" method="put" action="/form">...</form>

DELETE NOT SUPPORTED!
<form id="form4" name="form4" method="delete" action="/form">...</form>

Crazy, huh?

When a browser user submits data to a server, nobody knows if they are creating, deleting, or modifying data. A POST can add or change an entity just like a PUT can, right? So, other than the identifying URL, the right action or HTTP Verb to use is most often POST. Therefore, PUT has always been optional in the traditional web world for decades.

When a form pushes data to the server, they are not honoring the HTTP VERBS as they were meant to be used. If you set up your server and HTML to follow conventions they would fail. AJAX or other JavaScript REST calls could override conventions and send a PUT meant for a POST for a given URL that is not an identifier type. But it would not matter today online.

To me, using PUT comes down to JavaScript use and the URI or Unique Identifier; the structure of the URL "posted" to the browser. The server then must look at the Http VERY bit also the URL to decide what the method is honored, then route the POST or PUT or DELETE etc. to the right place or process the resource changes as required. Only this then honors the Post vs Put design.

It's the flawed nature of today's client-side model with its broken HTML5 design, plus the corrupting nature of today's thick-client JavaScript API calls and other circus-tricks, that have polluted what should have been a very simple HTTP Standard created long ago by very forward looking people. But adding PUT to today's mix, and honoring its narrow use with scripting and unique identifiers will help to clean up the messy web we have today.


41
[+1] [2022-07-01 16:05:56] Ahmad Ismail

From the looks of it, both POST and PUT are same. However, they have some differences.

In HTTP Essentials: Protocols for Secure, Scaleable Web Sites, the author says:

The difference between POST and PUT is in how the server interprets the Uniform Resource Identifier. With a POST , the uri identifies an object on the server that can process the included data. With a PUT , on the other hand, the uri identifies an object in which the server should place the data. While a POST uri generally indicates a program or script, the PUT uri is usually the path and name for a file.

The author suggests that we use PUT to upload files, not POST. POST is for submitting forms.

Implemented correctly, the GET, HEAD, PUT, and DELETE method are idempotent, but not the POST method. So, when you make two PUT - you get the one new record, when you do two POSTs - you get two new records.

However, please note that, HTML forms only support GET and POST as HTTP request methods [1].

<form method="put"> is invalid HTML and will be treated like , i.e. send a GET request. [2]

[1] https://stackoverflow.com/a/166501/1772898
[2] https://stackoverflow.com/a/8054241/1772898

42