Reputation: 150
Should a RESTful (or any) type of server only use POST routes for data creation? For example, if I were to say I needed to get some filtered data from our server, some people would suggest putting these conditions in the body of a POST route.
However, some developers would also suggest to put those conditions in a GET route and use either url parameters or url queries to fetch data. The same developers on the latter side would also tell me that whenever a POST route is called, then something should be created, like in the database for example.
To me it seems like it would be better practice to use GET for queries to keep it RESTful. I would like to know how important it is to keep POST routes out of basic queries and why? (if it is important at all)
Upvotes: 0
Views: 60
Reputation: 12310
The main feature of REST, as defined by Fielding, is uniform interface.
To have a uniform interface, you need to standardize the protocol elements you’re using.
The common standard for HTTP methods is RFC 7231. It defines GET
for retrieval of representations, which can include “filtered data”. It defines GET
to be safe and cacheable.
Imagine you are writing a universal RESTful client. You can assume that GET
will retrieve a representation of the requested URL. You can cache responses to GET
. You can retry GET
requests on errors (because it is safe). That’s all very useful.
But POST
is defined as a generic, catch-all method. Not only does it lack any semantics of its own, but RFC 7231 says (emphasis mine):
The POST method requests that the target resource process the representation enclosed in the request according to the resource's own specific semantics.
Thus, in your universal RESTful client, you can’t assume anything about POST
, so you can’t do anything useful with it.
So GET
is part of a uniform interface, while POST
is not. In that sense, GET
is “more RESTful”.
That said, you could devise a new standard for POST
requests e.g. with Content-Type: application/data-filter
, with useful and well-defined semantics. It would contradict the above text of RFC 7231, but whatever, if you get enough people to agree with you (perhaps within your organization), you’ve got a uniform interface again.
By the way, there has been an attempt to standardize a SEARCH
method for something like this. That would work fine as a uniform interface (in conjunction with a standardized Content-Type
). Unfortunately, that proposal has stalled, so you probably shouldn’t use SEARCH
.
Upvotes: 3