如何设计需要大量数据的DELETE REST API?

发布于 2025-01-11 13:33:09 字数 186 浏览 0 评论 0原文

我想实现 DELETE REST API。但我需要提供要删除的 ID 列表的选项。该列表可以任意长,并且可能无法容纳在 URL 中。

我知道 POST 支持这一点,但对此和 DELETE 的支持似乎有争议。我想知道其他人是如何处理这个案子的。

如何设计 API 来处理这种情况?

I want to implement a DELETE REST API. But I need the option to provide a list of IDs to be deleted. This list could be arbitrarily long and may not fit within a URL.

I know POST supports this, but support for this and DELETE seems debatable. I wonder how others are handling this case.

How would an API be designed to handle this case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终遇你 2025-01-18 13:33:09

不幸的是,这是 REST 最大的限制之一,但有一些方法可以解决它。

在本例中,我将抽象出一个新实体 DeletionRequest,并将其与适当的 ID 一起发布或放置。由于它是一个新实体,因此它有自己的休息端点。

这样做的一个很好的副作用是端点和实体可以扩展以支持异步请求。如果您想删除大量数据,您不希望依赖于单个请求中发生的数据,因为诸如超时之类的事情可能会妨碍您。通过 DeletionRequest,用户可以在第一次推送时获取删除请求的 ID,然后使用 GET 请求检查状态。在幕后,您可以使用异步系统(celery、sidekiq 等)来实际删除内容并更新 DeletionRequest 的状态。

当然,您不必走那么远才能开始,但这将允许您朝这个方向扩展应用程序,而无需更改 API。

This is unfortunately one of the biggest limitations in REST, but there are ways around it.

In this case I would abstract out a new entity, DeletionRequest, and have that get posted or put with the appropriate IDs. Since it is a new entity it would have its own rest endpoints.

A nice side effect of this is that the endpoints and entity can be expanded out to support async requests. If you want to delete a ton of data you don't want to rely on it happening in a single request, as things like timeouts can get in the way. With a DeletionRequest the user can get an ID for the deletion request on the first push, and then check the status with a GET request. Behind the scenes you can use an async system (celery, sidekiq, etc) to actually delete things and update the status of the DeletionRequest.

You don't have to take it that far to start, of course, but this would allow you to expand the application in that direction without having to change your API.

べ映画 2025-01-18 13:33:09

URI 是资源标识符,因此我认为 DELETE 不应包含正文,即使您可以在客户端和服务器上执行此操作。您可以在 URI 中发送数据,也可以在 DELETE 之前发送数据。

我在这里看到 3 个选项,但也许还有其他选项:

  • 按照 Robert 所说的操作并 POST 事务资源,而不是像 DeletionRequest 那样。
  • 将要删除的资源分组并删除整个组。
  • 对您想要删除的资源集合进行大规模的破解和修补。

The URI is the resource identifier, so in my opinion the DELETE should not contain a body even if you can do it with your client and server. Either you send your data in the URI or you send it prior the DELETE.

I see 3 options here, but maybe there are others:

  • Do what Robert says and POST a transaction resource instead like DeletionRequest.
  • Group the resources you want to delete and DELETE the entire group.
  • Do a massive hack and PATCH the collection of resources you want to delete from.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文