【问题标题】:syntax difference between PUT and POST methodPUT 和 POST 方法之间的语法差异
【发布时间】:2014-05-30 13:13:32
【问题描述】:

我正在编写一个 RESTful API 并尝试使用所有可用的 http 方法,但 PUT 方法有问题。

当我使用 put 方法发送 http 请求时,出现“400 Bad request”错误。 如果我使用 POST 方法,我没有问题。

这是我的 http PUT 请求:

Remote Address:::1:8080
Request URL:http://localhost:8080/adminRight
Request Method:PUT
Status Code:400 Mauvaise Requête

Request Headersview parsed
PUT /adminRight HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 37
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
Content-Type: application/json
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: JSESSIONID=41D1CCDF94D3150F0FCA3754E347A4AD

Request Payload
typeList=1&id=2&nom=labelViewerAvance

Response Headersview parsed
HTTP/1.1 400 Mauvaise Requête
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Length: 984
Date: Fri, 30 May 2014 12:55:32 GMT
Connection: close

这里是我的 http POST 请求:

Remote Address:::1:8080
Request URL:http://localhost:8080/adminRight
Request Method:POST
Status Code:200 OK

Request Headersview parsed
POST /adminRight HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 37
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
Content-Type: application/json
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: JSESSIONID=41D1CCDF94D3150F0FCA3754E347A4AD

Request Payload
typeList=1&id=2&nom=labelViewerAvance

Response Headersview parsed
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/json;charset=utf-8
Content-Length: 2
Date: Fri, 30 May 2014 13:09:03 GMT

PUT 和 POST 语法有什么区别?或者,它是我 web.xml 中的一种特殊配置吗?

提前感谢您的帮助。

使用新信息进行编辑:

我的请求通过这两种方法映射到 java 中:

@RequestMapping(value = "/adminRight", 
                method = RequestMethod.PUT
                )
     @ResponseBody
        public ResponseEntity<String> updateListRights(@RequestParam(value = "typeList") String typeList,
                @RequestParam(value = "id") String idList,
                @RequestParam(value = "nom") String nomList)
        {

@RequestMapping(value = "/adminRight", 
                method = RequestMethod.POST
                )
     @ResponseBody
        public ResponseEntity<String> addNewListRights(@RequestParam(value = "typeList") String typeList,
                @RequestParam(value = "id") String idList,
                @RequestParam(value = "nom") String nomList)

        {

【问题讨论】:

    标签: rest spring-mvc httprequest put


    【解决方案1】:

    您的 Server: Apache-Coyote/1.1 只是一个 HTTP 连接器。在该连接器后面有一个 Web 服务器,例如 Apache Tomcat。您必须查阅该服务器的手册并检查如何允许 HTTP 方法。通过Tomcat there is a server.xml file,有这样的东西:

    // Sample Security Constraint
     <security-constraint>
     <web-resource-collection>
      <web-resource-name><strong>restricted methods</strong></web-resource-name>
      <url-pattern>/*</url-pattern>
      <http-method>PUT</http-method>
      <http-method>POST</http-method>
      <http-method>DELETE</http-method>
      <http-method>OPTIONS</http-method>
      <http-method>TRACE</http-method>
     </web-resource-collection>
     <auth-constraint />
     </security-constraint>
    

    您应该将 PUT 和 DELETE 添加到该列表中。如果您的 REST 客户端在浏览器中运行并且它们在不同的域下提供服务,那么您必须启用 OPTIONS 方法(对于CORS preflight 请求),并添加CORS allow headers。通过为浏览器提供服务,您还必须添加some HTTP response headersset them properly 以防止XSS attacks

    另一个安全问题,你应该hide the version number coyote 连接器。

    顺便说一句。使用像Cookie: JSESSIONID=41D1CCDF94D3150F0FCA3754E347A4ADis not RESTful这样的会话cookie。

    我对 java 请求映射知之甚少,但是通过 REST,您通常使用 POST 将新项目资源添加到集合资源中,例如 POST /rights 在您的情况下,PUT 通常用于编辑整个项目资源,例如PUT /rights/{id},其中{id} 应该是唯一的资源ID(可能与您的聚合ID 之一相同)。在您的代码中,我看不到 PUT 请求与此 URL 结构相关的任何内容。你也可能对in PATCH 感兴趣。

    【讨论】:

      猜你喜欢
      • 2019-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      • 2012-12-11
      相关资源
      最近更新 更多