现在的位置: 首页 > 综合 > 正文

Restful服务中Delete请求能否使用Entity body

2013年09月12日 ⁄ 综合 ⁄ 共 1506字 ⁄ 字号 评论关闭

关于HTTP各个方法在Restful API中的使用,存在一些误区。

比如PUT/POST各自在什么场合使用,Conditional GET/Partial GET何时使用,PUT/DELETE方法在互联网传递时会不会遇到防火墙的问题。

这里主要说明一下DELETE方法,DELETE方法是用来删除URL所指定的资源的,作为HTTP协议规定的方法之一,当然可以被使用,只是需要注意下面的一些细节,避免系统设计意外。

1、具体实现上,服务器可能会有自己变通的处理方式,比如出于安全、数据完整性的考虑,把`删除`转义为`禁用`,毕竟删除数据是危险的,需要意识到这一点

2、提交DELETE请求时,不能包含Entity Body。排除传输编码的要求,Entity Body实际上就是HTTP消息体:

The message-body (if any) of an HTTP message is used to carry the entity-body associated with the request or response. 
The message-body differs from the entity-body only when a transfer-coding has been applied,

       message-body = entity-body
                    | <entity-body encoded as per Transfer-Encoding>

对于GET/POST/PUT方法,传递Entity Body都不会有问题,但对于DELETE方法,由于该方法传递Entity Body没有明确定义的语义,所以有些服务器实现会丢弃/忽略DELETE请求的entity body,或者拒绝该请求。参见HTTP协议草稿(http://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-22#page-28)4.3.5 DELETE章节:

   A payload within a DELETE request message has no defined semantics;
   sending a payload body on a DELETE request might cause some existing
   implementations to reject the request.


注意(过时的)老版本的RFC2616(http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.3)里面没有特别说明这一点。

3、PUT/DELETE方法从语义上来讲,对资源是有破坏性的,PUT更改现有的资源,而DELETE摧毁一个资源,带有破坏性质的方法有时候会被防火墙或者IT管理人员特别关注,所以当提供PUT/DELETE接口服务时,需要确保服务器前置的防火墙没有block这些方法。当然如果走的是HTTPS协议,无需担心这一点。

参考链接:

http://stackoverflow.com/questions/299628/is-an-entity-body-allowed-for-an-http-delete-request

http://stackoverflow.com/questions/1828790/restful-put-and-delete-and-firewalls

http://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-22#page-28

http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.3

by iefreer

抱歉!评论已关闭.