Servlets 3.1:如何在DELETE请求中处理正文 [英] Servlets 3.1: how to handle body in DELETE request

查看:78
本文介绍了Servlets 3.1:如何在DELETE请求中处理正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个服务器来处理来自javascript注释包(annotatorjs.org)的http请求。 javascript向服务器发送一个HTTP DELETE请求,该请求包含路径中对象的id,并且还将请求正文中的注释发送给JSON对象。



我试过用Java Web Services对象和普通的servlet实现DELETE请求。在这两种情况下,当我在没有正文的情况下提出请求时,它会起作用,但是当我添加一个正文时,我得到一个400错误请求。



这是我的servlet

  @WebServlet(/ api / store / delete / *)
公共类AnnotatorServlet扩展HttpServlet {

@Override protected void doDelete(HttpServletRequest请求,HttpServletResponse响应)
抛出ServletException,IOException {
System.out.println(received DELETE request,requestURI =+ request.getRequestURI() );


我的curl命令,没有主体成功:



curl -i -X DELETE http:/ / localhost:8080 / text / api / store / delete / 555608203004e74adbf65343
HTTP / 1.1 200 OK
服务器:GlassFish服务器开源版4.1
X-Powered-by-Servlet /3.1 JSP / 2.3(GlassFish Server开源版4.1 Java /甲骨文公司/ 1.8)
日期:2015年5月19日星期二19:35:08 GMT
Content-Length:0



我的curl命令与body失败:



curl -i -X DELETE -d @test.json
HTTP / HTTP:// localhost:8080 / text / api / store / delete / 555608203004e74adbf65343rel =nofollow> http / 1.1 400错误请求
服务器:GlassFish服务器开源版4.1
X-Powered-By:Servlet / 3.1 JSP / 2.3(GlassFish Server开源版4.1 Java / Ora cle Corporation / 1.8)
日期:2015年5月19日星期二19:35:16 GMT
连接:关闭
内容长度:0

由于DELETE方法显式地不定义有效载荷( RFC 7231 section 4.3.5 ),它应该遵循的相关指南是 RFC 7230第3.3节


请求中消息正文的存在由
Content-Length或Transfer-Encoding标题字段。请求消息
framing与方法语义无关,即使方法
没有为消息体定义任何用途。


适合服务器的符合标准的行为是消费和忽略有效负载,或者以某种特定于API的方式对其执行操作。 API特定的方式可能会发送400或任何其他状态代码。



您正在使用的第二个卷曲测试似乎没有将任何JSON数据发送到API。当我运行它只是发送测试字符串test.json作为有效载荷。所以不清楚服务器在拒绝什么(有效载荷存在?或未知的有效载荷格式?)。

如果您可以设计一个能够正确证明服务器拒绝的测试仅仅是有效载荷的存在,那么你应该向服务器提交一个错误。


I am writing a server for handling http requests from a javascript annotation package (annotatorjs.org). The javascript sends an HTTP DELETE request to the server that includes the id of the object in the path, and also sends the annotation in the body of the request in a JSON object.

I've tried implementing the DELETE request both with a Java Web Services object, and a plain servlet. In both cases, when I make the request without a body, it works, but when I add a body, I get a 400 Bad Request.

Here is my servlet

@WebServlet("/api/store/delete/*")
public class AnnotatorServlet extends HttpServlet {  

    @Override protected void doDelete( HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            System.out.println("received DELETE request,requestURI="+request.getRequestURI());        
    }
} 

My curl command, without body succeeds:

curl -i -X DELETE http://localhost:8080/text/api/store/delete/555608203004e74adbf65343 HTTP/1.1 200 OK Server: GlassFish Server Open Source Edition 4.1 X-Powered-By: Servlet/3.1 JSP/2.3 (GlassFish Server Open Source Edition 4.1 Java/Oracle Corporation/1.8) Date: Tue, 19 May 2015 19:35:08 GMT Content-Length: 0

My curl command with body fails:

curl -i -X DELETE -d @test.json http://localhost:8080/text/api/store/delete/555608203004e74adbf65343 HTTP/1.1 400 Bad Request Server: GlassFish Server Open Source Edition 4.1 X-Powered-By: Servlet/3.1 JSP/2.3 (GlassFish Server Open Source Edition 4.1 Java/Oracle Corporation/1.8) Date: Tue, 19 May 2015 19:35:16 GMT Connection: close Content-Length: 0

解决方案

Since the DELETE method explicitly does not define a payload (RFC 7231 section 4.3.5), the relevant guideline it should be following is RFC 7230 section 3.3:

The presence of a message body in a request is signaled by a Content-Length or Transfer-Encoding header field. Request message framing is independent of method semantics, even if the method does not define any use for a message body.

The proper standards compliant behaviour for the server is to either consume and ignore the payload, or to act on it in some API specific way. That "API specific way" may be sending 400 or any other status code.

That second curl test you are using does not appear to send any JSON data to the API. When I run it just sends the test string "test.json" as the payload. So it is not clear what the server is rejecting (payload existence? or unknown payload format?).

If you can devise a test which properly demonstrates that the server is rejecting on the mere existence of payload, then you should file a bug against the server.

这篇关于Servlets 3.1:如何在DELETE请求中处理正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆