【问题标题】:cURL POST to rest endpoint cause error at servercURL POST 到 rest 端点导致服务器错误
【发布时间】:2015-09-20 14:35:55
【问题描述】:

我正在尝试向本地运行的休息端点发出 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 插件 我从我的 REST 端点收到此帖子正文的预期响应。

我在我的 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 请求会发生这种情况。欢迎任何建议。

【问题讨论】:

    标签: rest post curl jersey-2.0


    【解决方案1】:

    好的,所以问题是我使用的是 Windows 机器,所以数据周围的单引号导致了这个问题。

    为了让它工作,我必须在数据周围使用双引号并转义数据中的双引号。

    curl -X POST -d "[{\"name\":\"xyz\",\"cost\":\"10\"}]" http://localhost:8080/xyz/rest/abc --header "Content-Type: application/json"  
    

    【讨论】:

    • 当 JSON 变大时,我喜欢只使用一个文件 -d @thefile.json。这样可以避免转义一堆字符。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-23
    • 1970-01-01
    • 1970-01-01
    • 2019-10-17
    • 1970-01-01
    • 2020-03-06
    相关资源
    最近更新 更多