这是我第一次编写Perl客户端来使用REST服务。我正在使用REST :: Client和JSON perl模块。 Web服务以JSON格式返回数据。问题是当我尝试在client-> responseContent()方法上使用from_json或decode_json方法时,我收到错误说
"格式错误的JSON字符串,无论是数组,对象,数字,字符串还是原子,都在字符偏移0处(在&#34之前; HTTP / 1.1 200 \ r \ n \ n ... ..."
Web服务是一个稳定的服务,可以与其他语言的REST客户端一起使用。
调试问题后,我发现client-> responseContent()不仅包含JSON数据,还包含头信息,因此from_json无法解析它。以下是代码片段:
my $url = "/data";
my $client = REST::Client->new();
$client->setHost($host);
my $headers = {Accept => 'application/json'};
$client->GET($url, $headers);
my $response = from_json($client->responseContent());
现在两天都无法弄清楚这件事:-(
以下是转储" $ client-> {_ res} - >转储"
Fri Feb 23 09:38:35 2018: HTTP/0.9 200 EOF
Client-Date: Fri, 23 Feb 2018 09:38:35 GMT
Client-Peer: 45.32.84.105:8282
Client-Response-Num: 1
HTTP/1.1 200 \r
Content-Type: application/json;charset=UTF-8\r
Transfer-Encoding: chunked\r
Date: Fri, 23 Feb 2018 09:38:33 GMT\r
Connection: close\r
\r
2000\r
[{"REGION":"AP","REMARK":null,"STATUS":"PROD","UPDATED_TIME":null,"UPDATED_BY":null,"ROUTE_ID":1,"ROUTE_ID_VER":20150310,"USER_ROUTE_LOGIC":"|CAPTIVE|","USER_DEST":null,"USER_ORDSIZE_TYPE":null,"MIN_USER_ORDSIZE_VAL":0,"MAX_USER_ORDSIZE_VAL":100,"TAG_775":"|1|","CROSS_CURRENCY":"|Y|N|","TAG_12703":"|PB-CS|","COUNTRY":"|AU|HK|ID|IN|JP|KR|MY|SG|","TAG_12207":...
(+ 423449 more bytes not shown)
即使传输编码没有分块,我也遇到了同样的问题;
Fri Feb 23 10:40:20 2018: HTTP/1.1 200 ^M
Content-Type: application/json;charset=utf-8^M
Content-Length: 1618^M
Date: Fri, 23 Feb 2018 10:40:20 GMT^M
Connection: close^M
^M
{ "data":[ {
"REGION" : "AP",
"REMARK" : "",
"STATUS" : "PROD",
"UPDATED_TIME" : "",
"UPDATED_BY" : "",
最终,现在使用curl命令解决了(下面的实际代码片段):
my $command = "curl '$url'";
my $rules = qx/$command/;
答案 0 :(得分:0)
我正在解决类似的问题。我怀疑(尚未验证)根本原因是服务器未传递Encoding-Content
响应标头,因此LWP::UserAgent
和HTTP::Response
没有尝试返回解码后的字符串。我将尝试隔离在哪里做出此决定,并查看可用的选项并请求补丁。