0

I am writing a rest api which will provide some information based on the inputs in the request body. I am a bit confused which http method should I use i.e GET or PUT or POST. As I know there will be request body so I am ruling out GET from this, as I did some research and found few server implementations may ignore request body for GET. Now , the question remains should I use POST or PUT. Considering the output of the API will remain same for same input provided (eg: if input is 1 and output is true, output will always remain true for input 1) which means the method should be idempotent, I am leaning more towards using PUT as compared to POST. Just want to confirm if I am thinking in the right direction. Would be grateful for any help provided over this.

CovetousSlope
  • 171
  • 11
  • `POST` is intended to create a new resource, `PUT` is is intended to mainly update a resource but the API may decide to also create it if it doesn't exist. `GET` is intended to retrieve resources. `GET ` also should not change state of a resource. More info [**here**](https://restfulapi.net/http-methods/) - When you found what fits your requirement best then look how you will have to send the parameters along with the request as some prefer one method over another. – Nope Mar 09 '20 at 08:31

1 Answers1

1

GET request should not have a request body in HTTP/1.1, more reading: HTTP GET with request body

PUT as the name suggests puts a resource somewhere, so it's not the request to choose either.

POST is what I would choose to do something like that.

Or you could parse the data into the URL for GET.

(More reading: What's the difference between a POST and a PUT HTTP REQUEST?)

(It's one of my first answers - please leave feedback so I could improve!)

Janko
  • 11
  • 3
  • I agree, I would also use either POST or convert the data to URL query parameters and use GET. An example where I use query parameters would be filling in a search form and on submit convert the data to query parameters and then use GET to get the result. – Beyers Mar 08 '20 at 16:26
  • Hello Janko and Beyers ,thanks for you time and answers, but still one question comes to my mind and that is should we not think about idempotency over here ? – CovetousSlope Mar 08 '20 at 17:15
  • Is the operation changing resource(s)? If not, then I would say GET is more appropriate than POST/PUT. If it is changing resource(s) and idempotent then PUT is more accurate from a REST purest point of view. I'm no REST purest and try to keep things as simple as possible: GET for no changes, POST for new resource, PUT to update resource, DELETE to delete resource. I would argue documenting the API usage is more important than sticking to strict REST rules... – Beyers Mar 08 '20 at 20:08