gpt4 book ai didi

java - 小服务程序 3.1 : how to handle body in DELETE request

转载 作者:行者123 更新时间:2023-12-02 08:27:53 24 4
gpt4 key购买 nike

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

我尝试过使用 Java Web 服务对象和普通 servlet 来实现 DELETE 请求。在这两种情况下,当我发出没有正文的请求时,它都有效,但是当我添加正文时,我收到 400 Bad Request。

这是我的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());
}
}

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

curl -i -X DELETE http://localhost:8080/text/api/store/delete/555608203004e74adbf65343HTTP/1.1 200 正常服务器:GlassFish Server 开源版 4.1X-Powered-By: Servlet/3.1 JSP/2.3 (GlassFish Server Open Source Edition 4.1 Java/Oracle Corporation/1.8)日期:2015 年 5 月 19 日星期二 19:35:08 GMT内容长度:0

我的 body curl 命令失败:

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

最佳答案

DELETE 不应该像 GET 一样有主体。

RFC:https://www.rfc-editor.org/rfc/rfc7231#section-4.3.5

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

最好的做法是,不要这样做。


有人可能会争辩说,这是服务器中的错误。但服务器也可以合法地声明,这是一种罕见的情况,它有权拒绝服务。

关于java - 小服务程序 3.1 : how to handle body in DELETE request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30334776/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com