简体   繁体   English

是一个没有数据的RESTful PUT“洁净”,还是应该始终使用DELETE?

[英]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.

相关问题 是否应该在 RESTful API 中使用视图 - Should Views be used in RESTful APIs HTTP状态应该用于Restful错误响应吗? - Should HTTP Status be used in Restful Error Responses? 在RESTful API中将PHP PUT数据发送到Json - PHP PUT data to Json in RESTful API 我应该在我宁静的api中加入连接词吗? - should I put connecting words in my restful api or not? RESTful API:删除实体 - 我应该返回什么结果? - RESTful API: Delete Entity - What should I return as result? RESTful API应该返回带有代码的数据还是将代码转换为描述? - Should a RESTful API return data with codes or translate codes into descriptions? REST API:是否应该使用PUT或DELETE来部分更新资源? - REST API: Should we use PUT or DELETE to update resource partially? 在RESTful应用程序中,我们如何区分“动作”和HTTP动词(GET,POST,PUT,DELETE)? - In a RESTful application, how do we differentiate between an “action” and an HTTP verb (GET, POST, PUT, DELETE)? 是否应该始终在HTTP PUT请求中的url和body中验证资源ID? - Should we always validate resource id in url and body in HTTP PUT request? GET,POST,PUT哪个应用于将json对象接收到我的rest api并作为响应发送json对象? - GET,POST,PUT Which should be used for receiving a json object to my rest api and send a json object in response?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM