Curl不适用于HTTP Post到Spring

我在Spring @Controller有一个RequestMapping设置,我无法正确地向它发送一个CURL请求。 尝试发送各种方式时,我不断收到HTTP 400返回码。 我不确定什么设置不正确。

这是请求

curl -v -X POST localhost:8082/api/registerDevice -d '{"serial":"FA541YJ05065"}' -H "Content-Type:application/json;charset=UTF-8"

这是输出

Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying ::1...
* Connected to localhost (::1) port 8082 (#0)
> POST /api/registerDevice HTTP/1.1
> Host: localhost:8082
> User-Agent: curl/7.49.0
> Accept: */*
> Content-Type:application/json;charset=UTF-8
> Content-Length: 23
>
* upload completely sent off: 23 out of 23 bytes
< HTTP/1.1 400 Bad Request
< Server: Apache-Coyote/1.1
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Thu, 16 Jun 2016 02:48:50 GMT
< Connection: close
<
{"timestamp":1466045330556,"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Could not read document: Unexpected character (''' (code 39)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')n at [Source: java.io.PushbackInputStream@3bae0839; line: 1, column: 2]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character (''' (code 39)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')n at [Source: java.io.PushbackInputStream@3bae0839; line: 1, column: 2]","path":"/api/registerDevice"}* Closing connection 0

Spring RequestMapping

 @RequestMapping(value = "registerDevice", method = RequestMethod.POST)
  public ResponseEntity<String> registerDevice(@RequestBody Map<String, Object> payload) throws Exception {

更新

如下所述,这是一个逃避报价和与Windows相关的问题。 相同的CURL命令在centos上运行良好。

curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d "{"""serial""":"""FA541YJ05065"""}" http://localhost:8082/api/deregisterDevice

在Centos上

curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"serial":"FA541YJ05065"}' http://localhost:8082/api/registerDevice

我怀疑你最终发送了'在json的开始。 您可以尝试在json周围使用双引号并在其中转义它们。 也可以看看

curl -v -X POST localhost:8082/api/registerDevice -d "{"serial":"FA541YJ05065"}" -H "Content-Type:application/json;charset=UTF-8"

这背后的唯一原因是,您的请求没有正确格式化查看这个Spring 4.x / 3.x(Web MVC)REST API和JSON2 Post请求,如何为所有人做好准备? 了解更多信息。

链接地址: http://www.djcxy.com/p/48453.html

上一篇: Curl not working for HTTP Post to Spring

下一篇: Spring MVC POST @RequestBody don't bind to JSON string in the http request