Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send a huge parameter list to a GET request

I have a REST API in my server, where the List operation (that should be implemented using the GET method) receives multiple parameters from the client:

  • The current page
  • The number of rows
  • A text for performing a quick search
  • An object that defines a complex filter for the search (set of rules in the form 'field op value')

Due to this complex object for filtering the search, I need to define the List as POST, what I think that it's not a good idea, as REST defines the list operation as GET.

My question is simple: there exists any way to solve this using a GET method, avoiding to call it with an huge URL with parameters?

like image 750
Marc Gil Sendra Avatar asked Apr 18 '18 07:04

Marc Gil Sendra


3 Answers

Thanks to your answers. It seems that this question is really concerning, because there is not a clear valid answer. It's up to the developer to decide how to deal with it.

  • REST says that you should use GET method for listing, but large URIs are very ugly. Is there any problem nowadays? It seems that there is no problem because the most of the browsers supports very large URIs (Internet Explorer, go home, you don't play this game)
  • You can use a PUT/POST method for listing too, but it seems that it doesn't accomplish the REST principles
  • You can use a GET method to pass the simple parameters, and attach the complex parameters in the body, but it doesn't accomplish the HTTP principles

So it seems that the best approach is the first one: use GET and build huge URIs.

like image 152
Marc Gil Sendra Avatar answered Oct 11 '22 08:10

Marc Gil Sendra


you can convert your object to json and then url-encode the json text string so you can put it in a single parameter.

To make your url-encoded json string shorter you could remove all the default values from your object prior to converting it to a json text string.

Long query strings in get requests are quite common, so no need to worry about those. There is a limit to how long a query string may become.

like image 24
Jonathan Avatar answered Oct 11 '22 09:10

Jonathan


I encountered a similar issue. I had to send a huge list, but I still had to use GET. I ended up encoding the string with an encoding algorithm and sending it like that. I decode the list in the backend. I also have a param which specifies if the call is made encoded or not and so, the endpoint can be used both encoded and un-encoded.

You can use this approach for multiple parameters as well. You can send your list of parameters like param1:value1,param2:value2 encoded and decode it on the backend.

Another approach I investigated was to use Base 62 for converting numbers.

like image 1
Marius Avatar answered Oct 11 '22 07:10

Marius