12

I am currently designing a JSON RESTful API which should have a boolean endpoint such as /item/vote which can either be false meaning that a user has not voted for a specific item or true meaning that he has voted.

I am currently struggling to select one out of multiple possible designs:

HTTP status codes

PUT /item/vote => set to true
DELETE /item/vote => set to false
GET /item/vote => status code 204 => true | status code 404 => false

JSON

PUT /item/vote HTTP/1.1
Content-Type: application/json

{
    "vote": true
}
GET /item/vote HTTP/1.1


HTTP/1.1 200 OK
Content-Type: application/json

{
    "vote": true
}

Is any of these two approaches better or is it just a matter of preference? I am currently preferring the HTTP status code approach because I have already seen it.

5
  • 2
    How do you distinguish a real 404 from a successful no vote?
    – user156792
    Mar 25, 2016 at 19:50
  • There is such thing. You can just vote. Similar to the (old) way of liking on Facebook.
    – 1' OR 1 --
    Mar 25, 2016 at 20:12
  • What is your mechanism by which you know that a GET request to a resource actually completed? How do you distinguish this case from a the very real case where the endpoint could simply be Not Found.
    – user156792
    Mar 25, 2016 at 20:20
  • 1
    In any case the resource should be named like a thing (voteStatus) and not like a verb vote.
    – usr
    Mar 25, 2016 at 20:59
  • 1
    vote can be a noun.
    – Eric Stein
    Mar 26, 2016 at 0:03

1 Answer 1

10

The second approach is highly preferable. PUT is intended to replace the resource on the server with the content you're PUTting. Likewise, DELETE is intended to delete a resource, not to set its value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.