简体   繁体   中英

Is a RESTful PUT with no data “kosher,” or should a DELETE always be used?

I have a RESTful route that works on an array field of a resource, such:

PUT /:id/mylist

When I do a PUT, I throw an error if the input is empty. That is, if an empty array is passed. I require at least one element in the array. So if the resource has an array of nine elements, and the route is called to PUT three, those three replace the existing nine.

But you cannot pass in no elements, because that would erase the nine and leave nothing.

Having no element IS ALLOWED, however - it just seems to me that allowing the array to be "cleared" in a PUT is wrong, and that it should only be done thusly:

DELETE /:id/mylist

Am I wrong? Are both okay? Is one preferred over the other?

I would think that doing DELETE on a list resource would infer that the list is no longer there and future GET requests to the URL would return a 404.

However, doing a PUT with an empty list would cause future GET requests to return a 200 and an empty list (however that is represented).

I would say both are valid approaches, it just depends one what are the most natural semantics for the resource.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM