删除使用REST多个记录 [英] Delete multiple records using REST

查看:300
本文介绍了删除使用REST多个记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是删除多个项目的REST-FUL方式?

我的用例是,我有一个骨干集合,其中我需要能够一次删除多个项目。这些选项似乎是:


  1. 发送为每一个记录的DELETE请求(如果有潜在的几十个项目,这似乎像一个坏主意);

  2. 发送删除其中ID的以删除URL串在一起(即/记录/ 1; 2; 3);

  3. 在非REST的方式,发送包含的ID标记为删除自定义的JSON对象。

所有的选项都不太理想。

这似乎是一个REST公约的一个灰色地带。


解决方案

  1. 是一个可行的选择REST风格,但显然具有与你所描述的限制。

  2. 请不要这么做。它将由中介机构作为意义解释; 2,删除(单个)资源在 /记录/ 1 3 - 因此,一个2xx应答,这可能它们会导致吹扫他们的的缓存/记录/ 1; 2; 3 ; 不可以清除 /记录/ 1 /记录/ 2 /记录/ 3 ;代理的 /记录/ 1 410响应; 2; 3。,或者其他的东西,不会从你的角度来看意义

  3. 这样的选择是最好的,的,可以做的REST风格。如果要创建一个API,你要允许质量变化的资源,你可以使用REST做到这一点,但究竟如何是不是很明显许多。一种方法是将创建变更请求的资源(例如,通过发布一个身如 =记录[1,2,3] /删除-请求)和轮询创建的资源(通过响应的位置头指定),如果发现您的请求被接受,拒绝,正在进行或已完成。对于长时间运行的操作是非常有用的。另一种方法是为发送 PATCH 请求列表资源 /记录中,体都包含资源和行动的清单,对这些资源(以任何格式要支持)执行。这是为快速操作,其中响应code代表请求可以指示操作的结果是有用的。

一切都可以同时REST的限制范围内保持实现,通常的答案是让问题变成了资源,并给它一个网址。结果
因此,批量操作,如删除此处,或张贴多个项目的列表,或作出同样的编辑资源裹,都可以通过创建一个批量操作列表,并可以发布您的新的操作对它进行处理。

不要忘了,休息不是解决任何问题的唯一办法。 REST只是一种建筑风格和你不这样做的有无的坚持(但你失去了互联网的一些好处,如果你不这样做)。我建议你​​往下看的<一个这个名单href=\"https://web.archive.org/web/20140917143640/http://nordsc.com/ext/classification_of_http_based_apis.html\"相对=nofollow> HTTP API架构并选择适合你的人。只是让自己意识到根据您的使用情况下,如果你选择其他架构,并作出明智的决定,你失去了什么。

有一些不好回答关于<一个这个问题href=\"http://stackoverflow.com/questions/511281/patterns-for-handling-batch-operations-in-rest-web-services\">Patterns在REST Web服务处理批量操作?这有太多太多的upvotes,但应该过于阅读。

What is the REST-ful way of deleting multiple items?

My use case is that I have a Backbone Collection wherein I need to be able to delete multiple items at once. The options seem to be:

  1. Send a DELETE request for every single record (which seems like a bad idea if there are potentially dozens of items);
  2. Send a DELETE where the ID's to delete are strung together in the URL (i.e., "/records/1;2;3");
  3. In a non-REST way, send a custom JSON object containing the ID's marked for deletion.

All options are less than ideal.

This seems like a gray area of the REST convention.

解决方案

  1. Is a viable RESTful choice, but obviously has limits as you have described.
  2. Don't do this. It would be construed by intermediaries as meaning "DELETE the (single) resource at /records/1;2;3" — So a 2xx response to this may cause them to purge their cache of /records/1;2;3; not purge /records/1, /records/2 or /records/3; proxy a 410 response for /records/1;2;3, or other things that don't make sense from your point of view.
  3. This choice is best, and can be done RESTfully. If you are creating an API and you want to allow mass changes to resources, you can use REST to do it, but exactly how is not immediately obvious to many. One method is to create a ‘change request resource’ (e.g. by POSTing a body such as records=[1,2,3] to /delete-requests) and poll the created resource (specified by the Location header of the response) to find out if your request has been accepted, rejected, is in progress or has completed. This is useful for long-running operations. Another way is to send a PATCH request to the list resource, /records, the body of which contains a list of resources and actions to perform on those resources (in whatever format you want to support). This is useful for quick operations where the response code for the request can indicate the outcome of the operation.

Everything can be achieved whilst keeping within the constraints of REST, and usually the answer is to make the "problem" into a resource, and give it a URL.
So, batch operations, such as delete here, or POSTing multiple items to a list, or making the same edit to a swathe of resources, can all be handled by creating a "batch operations" list and POSTing your new operation to it.

Don't forget, REST isn't the only way to solve any problem. "REST" is just an architectural style and you don't have to adhere to it (but you lose certain benefits of the internet if you don't). I suggest you look down this list of HTTP API architectures and pick the one that suits you. Just make yourself aware of what you lose out on if you choose another architecture, and make an informed decision based on your use case.

There are some bad answers to this question on Patterns for handling batch operations in REST web services? which have far too many upvotes, but ought to be read too.

这篇关于删除使用REST多个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆