In HTTP, does PUT and POST send data differently?

14,294

Solution 1

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

Solution 2

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

Solution 3

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

Share:
14,294
DexCurl
Author by

DexCurl

Updated on July 12, 2022

Comments

  • DexCurl
    DexCurl almost 2 years

    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!

  • DexCurl
    DexCurl over 12 years
    Interesting, I've read that POST is used to update and PUT is used to create, such in the accepted answer here stackoverflow.com/questions/630453/put-vs-post-in-rest
  • jondavidjohn
    jondavidjohn over 12 years
    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 ... 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.
  • Chris Morlier
    Chris Morlier over 11 years
    Here is a pretty good expansion on what you're saying: PUT or POST the Rest of the Story.
  • Tushar Banne
    Tushar Banne about 5 years
    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?