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 plex filter for the search (set of rules in the form 'field op value')
Due to this plex 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?
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 plex filter for the search (set of rules in the form 'field op value')
Due to this plex 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?
Share Improve this question edited Apr 15, 2024 at 11:13 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Apr 18, 2018 at 7:34 Marc Gil SendraMarc Gil Sendra 9312 gold badges10 silver badges23 bronze badges 3- 1 How you define huge in terms for URL parameter size, is a bit of a concern. Take a look at stackoverflow./a/417184/3126973. So, I don't think that your plex object could be passed through URL as a GET parameter. – Romeo Sierra Commented Apr 18, 2018 at 7:38
- 1 Have a look at this post link. TLDR: while you can send a body with a GET request, it's not remended and not defined in the specs. – Ron Nabuurs Commented Apr 18, 2018 at 7: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 Commented Apr 18, 2018 at 7:43
3 Answers
Reset to default 7Thanks 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 acplish the REST principles
- You can use a GET method to pass the simple parameters, and attach the plex parameters in the body, but it doesn't acplish the HTTP principles
So it seems that the best approach is the first one: use GET and build huge URIs.
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 mon, so no need to worry about those. There is a limit to how long a query string may bee.
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.