简体   繁体   English

我们可以将参数传递给 HTTP DELETE api

[英]Can we pass parameters to HTTP DELETE api

I have an API that will delete a resource (DELETE /resources/{resourceId})我有一个 API 将删除一个资源 (DELETE /resources/{resourceId})

THE above API can only tell us to delete the resource.上面的 API 只能告诉我们删除资源。 Now I want to extend the API for other use cases like taking a backup of that resource before deleting or delete other dependant resources of this resource etc. I want to extend the delete API to this (DELETE /resources/{resourceId}?backupBeforeDelete=true...)现在我想将 API 扩展为其他用例,例如在删除或删除此资源的其他依赖资源之前备份该资源等。我想将删除 API 扩展到此 (DELETE /resources/{resourceId}?backupBeforeDelete=真的...)

Is the above-mentioned extension API good/recommended?上述扩展 API 好/推荐吗?

According to the HTTP Specification , any HTTP message can bear an optional body and/or header part, which means, that you can control in your back-end - what to do (eg see what your server receives and conventionally perform your operation), in case of any HTTP Method;根据HTTP 规范,任何 HTTP 消息都可以带有可选的正文和/或 header 部分,这意味着执行您的操作(例如,您可以在后端控制和常规接收什么)在任何 HTTP 方法的情况下; however, if you're talking about RESTful API design, DELETE, or any other operation should refer to REST API endpoint resource, which is mapped to controller's DELETE method, and server should then perform the operation, based on the logic in your method.但是,如果您在谈论RESTful API设计、DELETE 或任何其他操作,则应参考 REST API 基于端点资源的方法逻辑,然后在您的方法中执行操作,然后在您的方法中执行操作。

DELETE /resources/{resourceId} HTTP/1.1

should be OK.应该可以。

Is the above-mentioned extension API good/recommended?上述扩展 API 好/推荐吗?

Probably not.可能不是。

HTTP is (among other things) an agreement about message semantics : a uniform agreement about what the messages mean . HTTP 是(除其他外)关于消息语义的协议:关于消息含义的统一协议。

The basic goal is that, since everybody has the same understanding about what messages mean, we can use a lot of general purpose components (browsers, reverse proxies, etc).基本目标是,由于每个人对消息的含义都有相同的理解,我们可以使用许多通用组件(浏览器、反向代理等)。

When we start trying to finesse the messages in non standard ways, we lose the benefits of the common interface.当我们开始尝试以非标准方式处理消息时,我们就失去了通用接口的好处。

As far as DELETE is concerned, your use case runs into a problem, which is that HTTP does not define a parameterized DELETE.就 DELETE 而言,您的用例遇到了一个问题,即 HTTP 没有定义参数化的 DELETE。

The usual place to put parameters in an HTTP message is within the message body.在 HTTP 消息中放置参数的通常位置是在消息正文中。 Unfortunately...很遗憾...

A payload within a DELETE request message has no defined semantics; DELETE 请求消息中的有效负载没有定义的语义; sending a payload body on a DELETE request might cause some existing implementations to reject the request在 DELETE 请求上发送有效负载正文可能会导致某些现有实现拒绝该请求

In other words, you can't count on general purpose components doing the right thing here, because the request body is out of bounds.换句话说,您不能指望通用组件在这里做正确的事情,因为请求正文超出了范围。

On the other hand另一方面

DELETE /resources/{resourceId}?backupBeforeDelete=true

This has the problem that general purpose components will not recognize that /resources/{resourceId}?backupBeforeDelete=true is the same resource as /resources/{resourceId} .这存在通用组件无法识别/resources/{resourceId}?backupBeforeDelete=true是与/resources/{resourceId}相同的资源的问题。 The identifiers for the two are different, and messages sent to one are not understood to affect the other.两者的标识符不同,发送给其中一个的消息不会影响另一个。

The right answer, for your use case, is to change your method token;对于您的用例,正确的答案是更改您的方法令牌; the correct standard method for what you are trying to do here is POST您在这里尝试执行的正确标准方法是 POST

POST serves many useful purposes in HTTP, including the general purpose of “this action isn't worth standardizing.” POST 在 HTTP 中有许多有用的用途,包括“此操作不值得标准化”的一般用途。 -- Fielding, 2009 ——菲尔丁,2009

You should use the "real" URI for the resource (the same one that is used in a GET request), and stick any parameters that you need into the payload.您应该使用资源的“真实”URI(与 GET 请求中使用的相同),并将您需要的任何参数粘贴到有效负载中。

POST /resources/{resourceId}

backupBeforeDelete=true

Assuming you are using POST for other "not worth standardizing" actions, there will need to be enough context in the request that the server can distinguish the different use cases.假设您将 POST 用于其他“不值得标准化”的操作,则请求中需要有足够的上下文,以便服务器可以区分不同的用例。 On the web, we would normally collect the parameters via an HTML form, the usual answer is to include a request token in the body在 web 上,我们通常会通过 HTML 表单收集参数,通常的答案是在正文中包含请求令牌

POST /resources/{resourceId}

action=delete&backupBeforeDelete=true

On the other hand, if you think you are working on an action that is worth standardizing, then the right thing to do is set to defining a new method token with the semantics that you want, and pushing for adoption另一方面,如果你认为你正在做一个值得标准化的动作,那么正确的做法是定义一个具有你想要的语义的新方法标记,并推动采用

MAGIC_NEW_DELETE /resources/{resourceId}

backupBeforeDelete=true

This is, after all, where PATCH comes from;毕竟,这是PATCH的来源; Dusseault et al recognized that patch semantics could be useful for all resources, created a document that described the semantics that they wanted, and shepherded that document through the standardization process. Dusseault 等人认识到补丁语义可能对所有资源都有用,创建了一个描述他们想要的语义的文档,并通过标准化过程引导了该文档。

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

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