简体   繁体   English

Curl不适用于HTTP发布到Spring数据休息的RequestMapping

[英]Curl not working for HTTP Post to Spring-data-rest RequestMapping

I have a RequestMapping setup in a spring @Controller and I am unable to correctly send a CURL request to it. 我在spring @Controller有一个RequestMapping设置,但无法正确向它发送CURL请求。 I keep getting a HTTP 400 return code when trying to send various ways. 尝试发送各种方式时,我一直收到HTTP 400返回代码。 I am not sure what is setup incorrectly. 我不确定设置错误。

Here is the request 这是要求

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

Here is the output 这是输出

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

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

Update 更新资料

As mentioned below it was an issue with escaping the quotes and related to windows. 如下所述,这是转义引号和与Windows相关的问题。 The same CURL command worked fine on centos. 相同的CURL命令在centos上运行良好。

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

On Centos 在Centos上

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

The only reason behind this is that fact that your request is not formatted correctly Check out this Spring 4.x/3.x (Web MVC) REST API and JSON2 Post requests, how to get it right once for all? 造成这种情况的唯一原因是您的请求格式不正确。请查看此Spring 4.x / 3.x(Web MVC)REST API和JSON2 Post请求,如何一次正确地处理它? for more information. 欲获得更多信息。

I suspect that you end up sending ' at the start of the json. 我怀疑您最终在json的开头发送了' You can try using double quotes around the json and escaping them in it. 您可以尝试在json周围使用双引号并将其转义。 See also 也可以看看

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM