Home:ALL Converter>POST to get REST resource - three approaches - which one would you recommend?

POST to get REST resource - three approaches - which one would you recommend?

Ask Time:2014-06-16T14:45:29         Author:coder_bro

Json Formatter

I have REST resource (Ex: Tickets). To be able to obtain a set of Tickets that match a given set of constraints (Ex: start date, end date, price and other criterion) a user will need to pass information. This information can be included as query parameters and the protocol can define:

GET: Tickets?start-date=date&end-date=date&price=someprice...

The set of constraints to pass could be a lot. In such situations, is it better to use a POST and pass the set of constraints as JSON object within the body?

POST: Tickets
Body:
 {
   "start-date": "date"
   "end-date" : "date"
   . . .
 }

What are the drawbacks of such an approach? Does it still agree with the REST guidelines?Ref: http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven

Another alternative is the client could create a new resource called "Constraints" on the server, obtain a constraint-id (ex:123) as a response. Then it could use:

GET: Tickets?constraints-id=123

But this will mean that the server will periodically have to expire and delete "Constraint" objects, as clients might keep creating those without completing the business flow (ex: without confirming a Ticket in the end)

A third approach could be still use POST, but not create any resource. We can use a URI scheme like this:

POST: Tickets\Constraints
Body:
   Body:
     {
       "start-date": "date"
       "end-date" : "date"
       . . .
     }
Response:
200 OK ...
Tickets

This will mean that allthough no resource was created on the server, the need to POST the constraints to obtain Tickets is still made clear.

Which of these approaches would you recommend? What is most intuitive? Or is other any other alternative you would recommend?

Author:coder_bro,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/24237951/post-to-get-rest-resource-three-approaches-which-one-would-you-recommend
Ross Taylor-Turner :

Simply according to the HTTP spec, a POST is not a valid method to send a large amount of data for a query, as the intention is that the body of the request is to be stored by the server in some way, which is not the case in your example.\n\nMy current project faced the same problem and we decided to go with the more correct GET with many templated query parameters. Despite supporting over a dozen query params which can be quite long in length, most servers specify a GET request maximum length of 8KB, which I would expect to be an ample amount. I suppose this limit could be reached if you were attempting to send a GET with a large amount of the same query parameter to describe a long list, but if this is this case then it would suggest taking a step back and seeing how this has become a requirement of the API.\n\nIn my opinion a GET is the most intuitive and clearest use, and definitely seems to be the \"correct\" RESTful implementation. If the size of the request is an issue for you and you control the environment you are deploying to, you can even increase your server's max request size.",
2014-06-24T10:27:15
yy