8

I need to expose a rest api which needs 4 parameters, as of now. I have two options:

1) GET request with 4 query params  
2) POST request with an Object passed that encapsulates 4 parameters.

If i use case 1), then what if in future, more parameters need to be sent thereby making URL lengthy as query parameters will be increased. If i use case 2), then rest guideline will be violated as POST only meant to create/update.

Please let me know what is best approach in this case.

Anand
  • 20,708
  • 48
  • 131
  • 198
  • 3
    why query parameters and not say path parameters? Basically you are violating get/post just by comparing parameter count and without understanding the semantics of it. – SMA Feb 20 '15 at 06:14
  • 1
    the parameters i need to pass are actually meant for filtering, that's why query param instead of path para, – Anand Feb 20 '15 at 06:18
  • What is the resource? Check that you are not trying to implement RPC over REST. –  Feb 20 '15 at 09:30
  • I don't have too much experience with REST, but I think that your number is like a threshold. If there would have been 3, I would have said GET. If there would have been 5, I would have said POST. I believe that it's important if you already have a long path. For example, if the base URL is `http://localhost:////`, then 4 parameters make it too long. In this case, use POST. – ROMANIA_engineer May 29 '15 at 18:15

2 Answers2

4

If you need to pass long parameters, or binary ones, you'd normally use HTTP POST requests, and include the parameters in the POST body.

As a rule, GET requests should be for read-only queries; they should not change the state of the server and its data. For creation, updating, and deleting data, use POST requests. (POST can also be used for read-only queries when complex parameters are required.)

Reference: http://rest.elkstein.org/2008/02/more-complex-rest-requests.html

Also, you can refer here: What is the best way to design a HTTP request when somewhat complex parameters are needed?

Community
  • 1
  • 1
Tushar Khanna
  • 428
  • 6
  • 22
  • I actually don't understand what do you mean by complex parameters..does it refer number of parameters to be sent? – Anand Feb 20 '15 at 06:48
  • @Anand : Have you gone through second link that i have posted above, contains complex characters. – Tushar Khanna Feb 20 '15 at 07:31
  • @Anand : For your information : Maximum length of HTTP GET request http://stackoverflow.com/questions/2659952/maximum-length-of-http-get-request – Tushar Khanna Feb 20 '15 at 07:37
-1

Filtering params can also be sent as header params in a get request as well.

Besides if necessary sending more request params doesn't harm.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
anurag gupta
  • 379
  • 1
  • 5