Boolean endpoint within a RESTful API

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

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.

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *