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.

asked Mar 25, 2016 at 19:36
5
  • 2
    How do you distinguish a real 404 from a successful no vote? Commented Mar 25, 2016 at 19:50
  • There is such thing. You can just vote. Similar to the (old) way of liking on Facebook. Commented 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. Commented 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. Commented Mar 25, 2016 at 20:59
  • 1
    vote can be a noun. Commented Mar 26, 2016 at 0:03

1 Answer 1

9

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.

answered Mar 25, 2016 at 20:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.