Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A "long" GET request with many parameters

I'm implementing a restful API interface, and was wondering about some limitations and best practices.

I have a GET request which retrieves a series of entries from a database table.

However my problem is, I allow my callers to pass in quite a complicated set of criteria to filter out what they want.

My situation is:

  • After serialization into a query string, my query string is well over the limit of a GET request.
  • I would like to maintain a RESTful API, and since I'm retrieving records, it seems like I should use a GET request.

If I do not want to violate the RESTful API, but would like to achieve my goal of passing over a long query string of parameters, what would be a best practices to solve this?

Any suggestions are welcome. Thank you!

like image 476
Thomas Cheng Avatar asked Sep 06 '16 16:09

Thomas Cheng


1 Answers

One solution is to do the filtering in the front-end, instead of back-end. So you get all the records via GET request but only show the user the filtered ones. (Of course you don't get the records user does not have permission to see, you filter them server-side)

Advantages:

  • A pure REST solution, you don't send POST request to GET something

  • You make 1 request at the beginning and as the user tries different filters, no additional requests are required

Disadvantages:

  • If the data set is too big, the request might consume a lot of network resource. But this will be a 1 time request since additional filtering won't require new requests. Also, gzipping the response reduces a JSON response's size significantly, so you can send a JSON response with thousands of records via a few hundred KB.

  • If data set is too big, the filtering operation might take a long time in the browser and freeze it temporarily.

So this solution is highly dependent on your use case, but it is one of the solutions. Maybe a hybrid approach might be useful, like doing some of the main filtering on the server side and the rest of the filtering on the client side.

like image 151
Canol Gökel Avatar answered Sep 26 '22 23:09

Canol Gökel