Why not use PUT for REST requests requiring a payload?

REST recommends that requests (rather than resource creation) be performed using the GET method. In some cases, the request data is too large or structured in such a way that it complicates the placement of the URL, and to solve this problem, the RESTful API is modified to support requests using tel.

It seems that for RESTful requests requiring authorities, you should use POST. Here are some examples:

Requests do not change the internal state of the system, but POST does not support idempotent operations. However, PUT is idempotent. Why doesn't the RESTful API use PUT with a body instead of a POST for requests that require a body?

NOTE. A popular question asks that (PUT vs POST) is preferred for creating a resource. This question asks why PUT is not used for queries requiring tel.

+6
source share
2 answers

No. PUT may be idempotent, but it also has a certain meaning. The request body in the PUT should be used to replace the resource in the URI.

With POST such assumptions are made. Please note that using a POST request means that the request may not be idempotent, in some cases it may still be.

However, you can do this with PUT , but for this you need to go through an extra hoop. Basically, you can create a โ€œquery resourceโ€ using PUT , and then use GET immediately after receiving the results of this query resource. Perhaps this was what you needed, but it is the most RESTful, because the result of the query may be related. (something is completely missing if you use POST requests).

+4
source

You should read the standard: http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

POST definition:

The POST method is used to request that the source server accept the object enclosed in the request as a new subordinate resource identified by the Request-URI in the query string.

An action performed by the POST method may not result in a resource that can be identified using a URI. In this case, either 200 (OK) or 204 (Without content) - the corresponding response status, depending on whether or not the response includes an object that describes the result.

Definition of PUT

The PUT method requests that the private object be stored under the provided Request-URI. If the Request-URI refers to an existing resource, the private object SHOULD be considered a modified version of the one located on the origin server. If the Request-URI does not indicate an existing resource and that the URI is capable of being identified as a new resource by the requesting user agent, the origin server can create a resource with this URI.

If the resource cannot be created or modified using the Request-URI, you must give an appropriate error response that reflects the nature of the problem.

Another thing is that PUT is not cached, but POST.

Responses to this method cannot be cached unless the response includes the appropriate Cache-Control or Expires header fields.

eg. http://www.ebaytechblog.com/2012/08/20/caching-http-post-requests-and-responses/

-2
source

All Articles