cURL POST rest 端点导致服务器错误
cURL POST to rest endpoint cause error at server
我正在尝试向我的本地 运行 休息端点发出 POST 请求,如下所示:
curl -X POST -d '[{"name":"xyz","cost":"10"}]' http://localhost:8080/xyz/rest/abc --header "Content-Type: application/json"
这导致
Unexpected character (''' (code 39)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@3180b68d; line: 1, column: 2]
当我使用其他工具时,例如。 Firefox 浏览器中的 RESTClient 插件我从我的休息端点得到了这个 post 正文的预期响应。
好吧,我在我的 Jersey 休息端点启用了服务器端请求记录:
RESTClient 请求导致:
15:48:31,916 INFO [org.glassfish.jersey.filter.LoggingFilter] (http--127.0.0.1-
8080-1) 1 * Server has received a request on thread http--127.0.0.1-8080-1
1 > POST http://localhost:8080/xyz/rest/abc
1 > accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
1 > accept-encoding: gzip, deflate
1 > accept-language: null
1 > cache-control: no-cache
1 > connection: keep-alive
1 > content-length: 91
1 > content-type: application/json; charset=UTF-8
1 > host: localhost:8080
1 > pragma: no-cache
1 > user-agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:40.0) Gecko/20100101 Fire
fox/40.0
15:48:32,086 INFO [org.glassfish.jersey.filter.LoggingFilter] (http--127.0.0.1-
8080-1) 1 * Server responded with a response on thread http--127.0.0.1-8080-1
1 < 201
1 < Content-Type: application/json
cURL 请求结果:
15:49:04,786 INFO [org.glassfish.jersey.filter.LoggingFilter] (http--127.0.0.1-
8080-2) 2 * Server has received a request on thread http--127.0.0.1-8080-2
2 > POST http://localhost:8080/xyz/rest/abc
2 > accept: application/json
2 > content-length: 76
2 > content-type: application/json
2 > host: localhost:8080
2 > user-agent: curl/7.33.0
15:49:04,792 INFO [org.glassfish.jersey.filter.LoggingFilter] (http--127.0.0.1-
8080-2) 2 * Server responded with a response on thread http--127.0.0.1-8080-2
2 < 400
2 < Content-Type: text/plain
现在,我知道我的休息端点工作正常,只是好奇为什么 cURL 请求会发生这种情况。欢迎提出任何建议。
好的,问题是我使用的是 windows 机器,所以数据周围的单引号导致了这个问题。
为了让它工作,我必须在数据周围使用双引号并转义数据中的双引号。
curl -X POST -d "[{\"name\":\"xyz\",\"cost\":\"10\"}]" http://localhost:8080/xyz/rest/abc --header "Content-Type: application/json"
我正在尝试向我的本地 运行 休息端点发出 POST 请求,如下所示:
curl -X POST -d '[{"name":"xyz","cost":"10"}]' http://localhost:8080/xyz/rest/abc --header "Content-Type: application/json"
这导致
Unexpected character (''' (code 39)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@3180b68d; line: 1, column: 2]
当我使用其他工具时,例如。 Firefox 浏览器中的 RESTClient 插件我从我的休息端点得到了这个 post 正文的预期响应。
好吧,我在我的 Jersey 休息端点启用了服务器端请求记录: RESTClient 请求导致:
15:48:31,916 INFO [org.glassfish.jersey.filter.LoggingFilter] (http--127.0.0.1-
8080-1) 1 * Server has received a request on thread http--127.0.0.1-8080-1
1 > POST http://localhost:8080/xyz/rest/abc
1 > accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
1 > accept-encoding: gzip, deflate
1 > accept-language: null
1 > cache-control: no-cache
1 > connection: keep-alive
1 > content-length: 91
1 > content-type: application/json; charset=UTF-8
1 > host: localhost:8080
1 > pragma: no-cache
1 > user-agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:40.0) Gecko/20100101 Fire
fox/40.0
15:48:32,086 INFO [org.glassfish.jersey.filter.LoggingFilter] (http--127.0.0.1-
8080-1) 1 * Server responded with a response on thread http--127.0.0.1-8080-1
1 < 201
1 < Content-Type: application/json
cURL 请求结果:
15:49:04,786 INFO [org.glassfish.jersey.filter.LoggingFilter] (http--127.0.0.1-
8080-2) 2 * Server has received a request on thread http--127.0.0.1-8080-2
2 > POST http://localhost:8080/xyz/rest/abc
2 > accept: application/json
2 > content-length: 76
2 > content-type: application/json
2 > host: localhost:8080
2 > user-agent: curl/7.33.0
15:49:04,792 INFO [org.glassfish.jersey.filter.LoggingFilter] (http--127.0.0.1-
8080-2) 2 * Server responded with a response on thread http--127.0.0.1-8080-2
2 < 400
2 < Content-Type: text/plain
现在,我知道我的休息端点工作正常,只是好奇为什么 cURL 请求会发生这种情况。欢迎提出任何建议。
好的,问题是我使用的是 windows 机器,所以数据周围的单引号导致了这个问题。
为了让它工作,我必须在数据周围使用双引号并转义数据中的双引号。
curl -X POST -d "[{\"name\":\"xyz\",\"cost\":\"10\"}]" http://localhost:8080/xyz/rest/abc --header "Content-Type: application/json"