简体   繁体   English

REST - HTTP DELETE - 语义 - 仅删除后代

[英]REST - HTTP DELETE - semantics - only delete descendants

In our project, a list of all books can be retrieved through REST:在我们的项目中,可以通过 REST 检索到所有书籍的列表:

GET http://server/api/books/

A specific book can be retrieved as following:可以按以下方式检索特定书籍:

GET http://server/api/books/:id/

Deleting a specific book is easy:删除特定书籍很容易:

DELETE http://server/api/books/:id/

Now, to my question: what should be the result of the following call:现在,对于我的问题:以下调用的结果应该是什么:

DELETE http://server/api/books/

Obviously, all books are deleted.显然,所有的书都被删除了。 But should the resource books/ also be deleted?但是资源书/也应该被删除吗? That is, after the request:也就是说,在请求之后:

  1. should GET /books/ return 200 OK with an empty list? GET /books/ 应该返回 200 OK 和一个空列表吗? or或者
  2. should GET /books/ return 404 not found? GET /books/ 是否应该返回 404 未找到?

According to the specs, which says that the concrete URI will be gone afterwards, I'd go for the second option.根据规范,具体的 URI 将在之后消失,我将 go 作为第二个选项。 However, this makes things complicated and unlogic, in my opinion.然而,在我看来,这使事情变得复杂和不合逻辑。 It makes more sense to have an empty list of books , instead of no books .有一个空的书单没有书更有意义。

What do you think?你怎么看?

If it makes you feel better, assume that the server has logic that recreates the books resource automatically after it has been deleted.如果它让您感觉更好,请假设服务器具有在删除图书资源后自动重新创建图书资源的逻辑。 :-) :-)

I'd go for 200 OK and an empty list.我会 go 获得 200 OK 和一个空列表。 If you really don't like that then create a new resource called /books/all如果你真的不喜欢这样,那么创建一个名为/books/all的新资源

and do

DELETE /Books/all

However, this makes things complicated and unlogic然而,这使事情变得复杂和不合逻辑

How?如何? You are requesting that a resource be deleted.您正在请求删除资源。 That resource is deleted.该资源被删除。 The result is that it isn't there any more.结果是它不再存在了。

If anything, it's confusing to have it still be present after you deleted it.如果有的话,在您删除它之后它仍然存在会令人困惑。

I think allowing DELETE /books is too risky.我认为允许DELETE /books风险太大。 Part of a well designed api is to avoid "easy" mistakes from api-client side.精心设计的 api 的一部分是避免来自 api 客户端的“简单”错误。 It could easily happen that in client code something is going wrong, accidently the id (eg empty-string variable) is missing and unintended DELETE /books is sent.很容易发生在客户端代码中出现问题,意外地丢失了 id(例如空字符串变量)并且发送了意外的DELETE /books

What I would do is to force the client to iterate through DELETE /books/{id} in case he wants to delete all books.我要做的是强制客户遍历DELETE /books/{id}以防他想删除所有书籍。

Maybe you can give more input on your use-case: I wonder how likely the use-case is that DELETE /books as root source called (it is quite radical to delete root resources).也许您可以就您的用例提供更多输入:我想知道用例将DELETE /books作为根源调用的可能性有多大(删除根资源是非常激进的)。 Maybe you are offering deleting a sub-resource, eg /user/{id}/shopping-cart/{id}/books .也许您提供删除子资源,例如/user/{id}/shopping-cart/{id}/books If it is a more "transient" resource (like shopping-cart is) deleting api for all books would make more sense.如果它是一个更“短暂”的资源(如购物车),删除所有书籍的 api 会更有意义。

Regarding your other question: For /books I would return 200 and empty list.关于您的其他问题:对于/books ,我将返回200和空列表。 In collection cases I much prefer empty-lists over 'null' values.在收集情况下,我更喜欢空列表而不是“空”值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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