A "long" GET request with many parameters
Asked Answered
K

1

6

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!

Kif answered 6/9, 2016 at 16:53 Comment(4)
Good answer here using POST and a header as Google Translate does: #19771531Notable
Possible duplicate of Design RESTful query API with a long list of query parametersCharacteristic
@ThomasDavidBaker Actually I think you're right about the answer that references Google's API. Using a POST and spoofing a GET method is probably the cleanest, simplest, and most seamless method.Kif
@AlexandruMarculescu Thanks for your answer. Though I do agree that the question is somewhat similar (not exactly same). Please allow me some time to reword it. Probably it will reference the link Thomas has posted though, as that seems to me is the best method i have seen and been suggested so far.Kif
B
4

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.

Battue answered 9/8, 2018 at 19:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.