2

I have to design a REST API in which a search request can take parameters for multiple Queries ( i.e. when the client make a call using this API, he should be able to send parameters to form multiple queries).

We have an existing API where we are using GET and it takes multiple parameters which together forms a single Query and then this API call returns the response for this query. e.g. currently I can pass firstName, lastName, age etc in the request and then get back the person.

But now I have to enhance this service(or have a separate service) where I should be able to send parameters like firstName1, lastName1, age1 to search person1 ; firstName2, lastName2, age2 to search person2 and so on.

Should I use POST for the new API and then send list of parameters(params for query1, params for query2 and so on)? Or is there a better approach.

We are using Spring Boot for REST implementation.

Rohit Gupta
  • 91
  • 1
  • 8

4 Answers4

1

The best thing to do here will be do POST and then return a JSON object with all the details of the Person in an array.

That way it will be faster and you would not have to deal with long urls for GET.

Also GET has limitations regarding the length of the request whereas there is no such limitation in case of POST.

Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
1

Its better to use POST because GET is good for 2,3 parameter but when you have a set of parameter or object then POST is Good.

Sumeet Tiwari
  • 343
  • 2
  • 11
0

It is really hard to give a right answer here. In general sending a GET request does have the advantage that you can leverage caching easily on a HTTP level, e.g. by using products like varnish, nginx, etc. But if you already can forsee that your URL including all params you'll have to send a POST request to make it work in all Browsers.

daniel.eichten
  • 2,535
  • 19
  • 26
0

RESTfull architecture should respect the principle of addressability.

Since multiple users can be accessed through a unique request, then ideally this group of user should get an address, which would identify it as a resource. However I understand that in the real world, URIs have a limited length (maximum length of HTTP GET request?). A POST request would indeed work well, but we lose the benefit of addressability.

Another way would be to expose a new resource : group,. Lets suppose that your current model is something like this :

.../users/{id}
.../users/search?{arg1}={val1};{arg2}={val2}

You could eventually do something like :

.../users/groups/
.../users/groups/{id}
.../users/search?group={id}

(explanation below)

then you could split your research in two :

  • first a POST on .../users/groups/ with, as proposed by other response, a JSON description of the search parameters. This request could scan the .../users/groups/ directory, and if this set of parameters exists, return the corresponding address .../users/groups/{id}. (for performance issues you could for instance define {id} with a first part which would give the number of users requested).
  • Then you could make a request for this group with a GET with something like this : .../users/search?group={id}.

This approach would be a bit more complex to implement, but is more consistent with the resource oriented paradigm.

Community
  • 1
  • 1
M. Timtow
  • 343
  • 1
  • 3
  • 11