Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why not use PUT for REST queries that require a payload?

Tags:

rest

REST recommends that queries (not resource creation) be done through the GET method. In some cases, the query data is too large or structured in a way that makes it difficult to put in a URL, and to address this, a RESTful API is modified to support queries with bodies.

It seems that the convention for RESTful queries that require bodies is to use POST. Here are a few examples:

  • Dropbox API
  • ElasticSearch
  • O'Reilley's Restful Web Services Cookbook

Queries don't modify the internal state of the system, but POST doesn't support idempotent operations. However, PUT is idempotent. Why don't RESTful APIs use PUT with a body instead of POST for queries that require a body?

NOTE: A popular question asks which (PUT vs POST) is preferred for creating a resource. This question asks why PUT is not used for queries that require bodies.

like image 671
lreeder Avatar asked Oct 19 '15 16:10

lreeder


1 Answers

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

With POST no such assumptions are being made. And note that using a POST request means that the request might not be idempotent, in specific cases it still might be.

However, you could do it with PUT, but it requires you to jump through an extra hoop. Basically, you could create a "query resource" with PUT, and then use GET immediately after to fetch the results of this query resource. Perhaps this was what you were after, but this is the most RESTful, because the resulting query results can still be linked to. (something which is completely missing if you use POST requests).

like image 106
Evert Avatar answered Oct 06 '22 17:10

Evert