Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GET vs POST to send some parameters in rest

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.

like image 278
Anand Avatar asked Nov 23 '22 01:11

Anand


1 Answers

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?

like image 199
Tushar Khanna Avatar answered Jan 10 '23 09:01

Tushar Khanna