Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In HTTP, does PUT and POST send data differently?

Tags:

http

post

put

api

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!

like image 998
DexCurl Avatar asked Dec 14 '11 17:12

DexCurl


People also ask

What is the difference between HTTP PUT and HTTP POST?

The difference between POST and PUT is that PUT requests are idempotent. That is, calling the same PUT request multiple times will always produce the same result. In contrast, calling a POST request repeatedly have side effects of creating the same resource multiple times.

Are put and POST the same?

Use PUT when you want to modify a single resource which is already a part of resources collection. PUT overwrites the resource in its entirety. Use PATCH if request updates part of the resource. Use POST when you want to add a child resource under resources collection.

What is the difference between the PUT and POST methods?

PUT method is call when you have to modify a single resource, which is already a part of resource collection. POST method is call when you have to add a child resource under resources collection. RFC-2616 depicts that the PUT method sends a request for an enclosed entity stored in the supplied request URI.

What happens if we use POST instead of put?

Another important difference between the methods is that PUT is an idempotent method, while POST isn't. For instance, calling the PUT method multiple times will either create or update the same resource. In contrast, multiple POST requests will lead to the creation of the same resource multiple times.


1 Answers

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
like image 85
kvc Avatar answered Sep 20 '22 14:09

kvc