最近在对接某 API,自称 RESTful... 遇到了这样的一个 delete 方法,定义如下:
其他的方法(除了 delete 相关)都是采用 POST 方式
不去吐槽接口的设计了,用起来还是挺方便的。有一个问题,http delete 请求是否可以携带 body?
查了一些资料,这个帖子讲得比较全面:http://stackoverflow.com/questions/299628/is-an-entity-body-allowed-for-an-http-delete-request
大体上说,没有明确禁止,即 http delete request 可以携带 body,但是一些 web server 可能会把 delete request 中的 body 给丢弃掉...
从接口设计角度讲,不该带着。
#http post
http = Net::HTTP.new(host, port)
response = http.post(uri, http_body, http_header)
#http delete with body
http = Net::HTTP.new(host, port)
req = Net::HTTP::Delete.new(uri.path, http_header)
req.body = http_body
response = http.request req
#http delete without body
http = Net::HTTP.new(host,port)
http.delete(uri, http_header)