[英]Updating more than one item in a RESTful way
What should be the correct RESTful approach when trying to update/delete more than one item? 尝试更新/删除多个项目时,正确的RESTful方法应该是什么?
Typical example: There is a list of items, the income folder of mails received, where you can select a group of them by setting the appropiate checkboxes. 典型示例:有一个项目列表,收到的邮件的收入文件夹,您可以通过设置适当的复选框选择一组。 Then you click in the "delete" button and send the petition of delete them.
然后单击“删除”按钮并发送删除请求。 What method should I call?
我应该用什么方法打电话? What parameters?
什么参数?
In this case it's pretty obvious I should use a DELETE
, but it only works with one item at once. 在这种情况下,很明显我应该使用
DELETE
,但它只能同时使用一个项目。 Also, I might not want to delete them, but storing them in a different folder, which might imply using a PATCH
, but then again, the PATCH
method only allows one item. 此外,我可能不想删除它们,但将它们存储在不同的文件夹中,这可能意味着使用
PATCH
,但是再次, PATCH
方法只允许一个项目。
In this case it's pretty obvious I should use a DELETE, but it only works with one item at once.
在这种情况下,很明显我应该使用DELETE,但它只能同时使用一个项目。 Also, I might not want to delete them, but storing them in a different folder, which might imply using a PATCH, but then again, the PATCH method only allows one item.
此外,我可能不想删除它们,但将它们存储在不同的文件夹中,这可能意味着使用PATCH,但是再次,PATCH方法只允许一个项目。
You should PATCH the collection (or a part of it) and not the item. 你应该修补集合(或它的一部分)而不是项目。 For example by moving items, you could use
PATCH /collection/?filter=x {location: newDir}
. 例如,通过移动项目,您可以使用
PATCH /collection/?filter=x {location: newDir}
。 The DELETE is a tricky question. DELETE是一个棘手的问题。 You can use the PATCH or an alternative to use the
DELETE /collection/?filter=x
, but that implies that you want to delete the collection resource instead of removing item resources from it. 您可以使用PATCH或替代方法来使用
DELETE /collection/?filter=x
,但这意味着您要删除集合资源,而不是从中删除项目资源。 By batch creation I think using the POST /collection/ [item1, item2, ...]
is okay. 通过批量创建我认为使用
POST /collection/ [item1, item2, ...]
是可以的。
Ofc. OFC。 you can use alternative URI structues too, for example
PATCH /books/1+2+3/ {price: 20, currency: "EUR"}
. 您也可以使用其他URI结构,例如
PATCH /books/1+2+3/ {price: 20, currency: "EUR"}
。 There is an alternative syntax for PATCH too, in that you add the operation name to the body, for example PATCH /collection/?filter=x {op: "update", location: newDir}
. PATCH还有一种替代语法,您可以将操作名称添加到正文中,例如
PATCH /collection/?filter=x {op: "update", location: newDir}
。 I don't like that, but if you want to reuse PATCH with the same URI, then it can become handy. 我不喜欢这样,但是如果你想重用具有相同URI的PATCH,那么它就会变得很方便。 Another alternative to send the request to a single batch endpoint eg
POST /transactions/
and use a multipart HTTP request. 将请求发送到单个批处理端点的另一种方法,例如
POST /transactions/
并使用多部分HTTP请求。
I think the most important part here to keep in mind, what you are doing is forcing multiple operations into a single transaction. 我认为最重要的部分是要记住,你正在做的是将多个操作强制转换为单个事务。 So if you don't need immediate consistency, and losing connection between requests is not a problem, then the client can send multiple requests parallel or series using a simple loop.
因此,如果您不需要立即一致性,并且丢失请求之间的连接不是问题,那么客户端可以使用简单循环并行或串行发送多个请求。
You could use matrix parameters for the delete. 您可以使用矩阵参数进行删除。 Given that the parameters are resource IDs you could use unnamed parameters, so for example a
DELETE
to https://host.com/messages/1;2;3
would remove messages with IDs 1, 2 and 3. 鉴于参数是资源ID,您可以使用未命名的参数,因此例如对
https://host.com/messages/1;2;3
的DELETE
将删除ID为1,2和3的消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.