7

From what I know you can send JSON data via POST, but should PUT be specifically sending information in the URI or can you do both?

Thanks!

Will Hartung
  • 115,893
  • 19
  • 128
  • 203
DexCurl
  • 1,683
  • 5
  • 26
  • 38

4 Answers4

15

Both POST and PUT can be used for create and update operations in different situations. So what exactly is the difference between PUT and POST? In a nutshell: use PUT if and only if you know both the URL where the resource will live, and the entirety of the contents of the resource. Otherwise, use POST.

POST is an incredibly general verb. Because it promises neither safety nor idempotence, and it has a relatively loosely-worded description in the RFC, you can use it for pretty much anything. In fact, you could make all of your requests POST requests because POST makes very few promises; it can behave like a GET, a PUT, or a DELETE if it wants to. It also can do some things that no other verb can do - it can create a new resource at a URL different from the URL in the HTTP request; and it can modify part of a resource without changing the whole thing (although the proposed but not widely-accepted PATCH method can do something similar).

PUT is a much more restrictive verb. It takes a complete resource and stores it at the given URL. If there was a resource there previously, it is replaced; if not, a new one is created. These properties support idempotence, which a naive create or update operation might not. I suspect this may be why PUT is defined the way it is; it's an idempotent operation which allows the client to send information to the server.

References:

  • RFC 2616 - HTTP 1.1
  • RFC 5789 - PATCH method for HTTP
  • Martin Fowler, the Richardson Maturity Model
kvc
  • 1,127
  • 2
  • 7
  • 21
  • 2
    Here is a pretty good expansion on what you're saying: [PUT or POST the Rest of the Story](http://jcalcote.wordpress.com/2008/10/16/put-or-post-the-rest-of-the-story/). – Chris Morlier Aug 24 '12 at 19:30
  • Are they(PUT and POST) technically different in terms of how they operate? In both cases, DB needs to be queried to check if record already exists. So what difference does it make? – Tushar Banne Apr 25 '19 at 20:07
2

From HTTP's point of view, the request format is the same.

Julian Reschke
  • 40,156
  • 8
  • 95
  • 98
0

PUT uploads a new resource on the server. If the resource already exists and is different, it is replaced; if it doesn't exist, it is created.

POST triggers an action on the server. It has side-effects and can be used to trigger an order, modify a database, post a message in a forum, or other actions.

Seif Tml
  • 2,413
  • 1
  • 17
  • 15
0

You can send the request body the same way, it is just handled differently by your application code...

The POST verb is traditionally used to create a resource

The PUT verb is traditionally used to update a resource

jondavidjohn
  • 61,812
  • 21
  • 118
  • 158
  • 1
    Interesting, I've read that POST is used to update and PUT is used to create, such in the accepted answer here http://stackoverflow.com/questions/630453/put-vs-post-in-rest – DexCurl Dec 14 '11 at 17:12
  • 1
    It's really a question of convention rather than standard, and I wouldn't rely on finding an "accepted" answer here as concrete evidence of **anything** ... http://stackoverflow.com/a/2447740/555384 ... if I were you I would peruse some popular Restful apis and see how they do it... I think you'll find this answer to be the most commonly accepted. – jondavidjohn Dec 14 '11 at 17:23
  • PUT should not be used to update a resource, you are probably thinking of PATCH. PATCH is used to partially modify a resource, which is close to the update concept in CRUD. The HTTP spec requires that PUT replace the target resource if it already exists, otherwise create it, which is very different from an update. – rjferguson Jan 26 '23 at 21:48