In HTTP, does PUT and POST send data differently?

DexCurl picture DexCurl · Dec 14, 2011 · Viewed 13.6k times · Source

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!

Answer

kvc picture kvc · Dec 14, 2011

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