[英]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: 我有一个适用于资源的数组字段的RESTful路由,例如:
PUT /:id/mylist 放置/:id / mylist
When I do a PUT, I throw an error if the input is empty. 进行PUT时,如果输入为空,则会引发错误。 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.
因此,如果资源具有九个元素的数组,并且路由被调用到PUT 3,则这三个将替换现有的九个元素。
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: 但是,不允许使用任何元素-在我看来,允许在PUT中“清除”数组是错误的,因此只能这样做:
DELETE /:id/mylist 删除/: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. 我认为对列表资源执行DELETE操作将推断该列表不再存在,并且将来对URL的GET请求将返回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). 但是,使用空列表进行PUT将导致将来的GET请求返回200和空列表(无论如何表示)。
I would say both are valid approaches, it just depends one what are the most natural semantics for the resource. 我想说这两种都是有效的方法,仅取决于一种最自然的资源语义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.