9

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?

Marc Gil Sendra
  • 819
  • 2
  • 9
  • 22
  • 1
    How you define *huge* in terms for URL parameter size, is a bit of a concern. Take a look at https://stackoverflow.com/a/417184/3126973. So, I don't think that your *complex object* could be passed through URL as a GET parameter. – Romeo Sierra Apr 18 '18 at 07:38
  • 1
    Have a look at this post [link](https://stackoverflow.com/questions/978061/http-get-with-request-body). TLDR: while you can send a body with a GET request, it's not recommended and not defined in the specs. – Ron Nabuurs Apr 18 '18 at 07:40
  • If I were you, I would send that object in the body of the request, but I would choose another idempotent HTTP method in order to respect the idempotent nature of the GET method (how about PUT?). – amedina Apr 18 '18 at 07:43

3 Answers3

7

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.

Marc Gil Sendra
  • 819
  • 2
  • 9
  • 22
2

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.

Jonathan
  • 1,355
  • 14
  • 22
1

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.

Marius
  • 11
  • 2