在发出HTTP DELETE请求时,请求URI应该完全标识要删除的资源。但是,是否允许添加额外的元数据作为请求实体主体的一部分?
当前回答
这个没有定义。
DELETE请求消息中的有效负载没有定义的语义; 在DELETE请求上发送有效负载主体可能会导致一些现有的问题 实现来拒绝请求。 https://www.rfc-editor.org/rfc/rfc7231#page-29
其他回答
实际答案:不
有些客户端和服务器会忽略甚至删除delete请求中的主体。在极少数情况下,它们会失败并返回错误。
只是一个提示,如果你在你的DELETE请求中提供了一个主体,并且使用了谷歌云HTTPS负载均衡器,它将拒绝你的请求,错误为400。我的头撞到墙上,然后发现谷歌,不管出于什么原因,认为一个带有主体的DELETE请求是一个畸形的请求。
规范并没有明确禁止或阻止它,所以我倾向于说它是允许的。
微软也是这么看的(我能听到听众的嘀嘀声),他们在MSDN的文章中提到了ADO的DELETE方法。NET数据服务框架:
如果一个DELETE请求包含一个实体体,该实体体将被忽略[…]
此外,这里是RFC2616 (HTTP 1.1)在请求方面所说的:
只有当消息体出现时,entity-body才会出现(第7.2节) 消息体的存在通过包含内容长度或传输编码头来表示(第4.3节)。 当请求方法的规范不允许发送实体体时(第4.3节),就不能包含消息体。 仅在TRACE请求中明确禁止使用实体体,所有其他类型的请求都不受限制(第9节,特别是9.8节)
对于响应,这已被定义为:
是否包含消息体取决于请求方法和响应状态(第4.3节) 明确禁止在HEAD请求的响应中使用消息体(第9节和9.4节) 1xx(信息)、204(无内容)和304(未修改)响应中明确禁止消息体(第4.3节) 所有其他响应都包含消息体,尽管消息体可能为零长度(第4.3节)
更新
在RFC 9110(2022年6月)中,明确了GET、HEAD和DELETE上的请求体不可互操作的事实。
9.3.5删除
Although request message framing is independent of the method used, content received in a DELETE request has no generally defined semantics, cannot alter the meaning or target of the request, and might lead some implementations to reject the request and close the connection because of its potential as a request smuggling attack (Section 11.2 of [HTTP/1.1]). A client SHOULD NOT generate content in a DELETE request unless it is made directly to an origin server that has previously indicated, in or out of band, that such a request has a purpose and will be adequately supported. An origin server SHOULD NOT rely on private agreements to receive content, since participants in HTTP communication are often unaware of intermediaries along the request chain.
也许下面的GitHUb url会帮助你,得到答案。 实际上,应用服务器,如Tomcat, Weblogic拒绝HTTP。带请求有效负载的DELETE调用。所以记住这些东西,我已经在github中添加了例子,请看看
https://github.com/ashish720/spring-examples
其他几个回答提到了RFC 7231,它有效地说了DELETE请求可以有一个主体,但不推荐。
在2022年,RFC 7231被RFC 9110: HTTP语义所取代,它现在说:
[...] content received in a DELETE request has no generally defined semantics, cannot alter the meaning or target of the request, and might lead some implementations to reject the request and close the connection [...]. A client SHOULD NOT generate content in a DELETE request unless it is made directly to an origin server that has previously indicated, in or out of band, that such a request has a purpose and will be adequately supported. An origin server SHOULD NOT rely on private agreements to receive content, since participants in HTTP communication are often unaware of intermediaries along the request chain.
这种语言在之前的语言基础上得到了加强,也就是说,即使它是允许的,在使用它时也需要非常小心,因为(例如)一些用户可能在代理的背后,为了打击“请求走私”而从请求中剥离主体。
推荐文章
- 有没有REST api的命名规范指南?
- 什么是HTTP中的“406-不可接受的响应”?
- 最好的轻量级web服务器(只有静态内容)的Windows
- HTTP POST在Java中使用JSON
- 哪些HTTP方法与哪些CRUD方法相匹配?
- 使用HTML形式的PUT方法
- 如何转义哈希字符在URL
- RESTful服务中部分更新的最佳实践
- JAX-RS / Jersey如何自定义错误处理?
- 有没有办法在python中做HTTP PUT
- 我能在服务器端应用程序(PHP、Ruby、Python等)上读取URL的哈希部分吗?
- 如何POST表单数据与Spring RestTemplate?
- 什么是http头“X-XSS-Protection”?
- 在nodejs http中body在哪里。得到回应?
- Restful API服务