Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put many request parameters with GET request in REST API [closed]

Tags:

java

rest

http

Is it a good practice to put many request parameters with GET request in REST API?

I have gone through couple of sites and tried to get the standard way of doing GET URI with parameters implementation.

Here's what i am trying to achieve : search for all users having provided all the search criteria.

Search criteria are like companyID, sections, offset, limit, orderby, filter.

As per basic standards GET request can not have request body or payload in it.

Will it be good way to make a GET request and put all parameters behind ? in the request URL, something like this :

GET http://localhost:8080/api/users?companyId=qwerty&sections=hr&offset=0&limit=20&oorderby=asc&filter=^[sSmM]

I was thinking to make a PUT or POST request and send all these data in payload, and implement the code to return the desired response, i.e list of users.

If i do this i am changing the default behavior of HTTP methods.

Can you please guide me a way to get out of the situation. Thanks.

like image 455
Madhusudan Joshi Avatar asked Mar 23 '23 23:03

Madhusudan Joshi


2 Answers

I think it does not matter how many parameters you put in a GET request. The purpose of a POST or PUT request is not to keep your URL clean, but to aline with a REST structure.

the raw definition of GET, POST and PUT are

  • GET: get a resource from your server
  • POST: create a resource on your server
  • PUT: update a resource on your server
like image 100
MatthiasLaug Avatar answered Apr 06 '23 04:04

MatthiasLaug


Actually you can pass a request body with a GET request. It's just not very common to do it. Jquery supports this on the browser side and REST APIs such as for example the elastic search API use this. They have a nice json based query dsl and you can actually use a GET request for these. Because some HTTP frameworks don't support passing a body with a GET, Elasticsearch offers a fallback with POST as well.

As for using lots of url parameters, there are two issues you should keep in mind:

  1. URLs don't have unlimited length. There are some differences between browsers for this but typically it is a few KB maximum. Particularly some older mobile browsers have limitations here. It's pretty easy to run into this limit if you put a lot of things in the request and basically this means urls get truncated.
  2. Long URIs look kind of ugly and you wouldn't necessarily want to show them in a web UI.
like image 41
Jilles van Gurp Avatar answered Apr 06 '23 04:04

Jilles van Gurp