【发布时间】:2017-01-31 18:19:08
【问题描述】:
我正在尝试按如下方式调用 REST Web 服务:
my $client = REST::Client->new();
$client->setHost("http://myhost.com");
$client->POST('/xx/yy/Submit',
$params,
{'Content-type' => 'application/json'}
);
my $response = $client->responseContent();
我不怎么创建参数列表,api定义如下:
{
"Credential": {
"Username": "String",
"Password": "String"
},
"DataCoding": "Default",
"Header": {
"From": "String",
"ValidityPeriod": 0
},
"Message": "String",
"To": ["String"]
}
我尝试了以下但没有工作:
my %params = (Credential => {
Username => $username,
Password => $password
},
DataCoding => 'Default',
Header => {
From => $from,
ValidityPeriod => 0
},
Message => "Test",
To => ['90535xxxx','90542xxxxx']
);
$client->POST('/v1/xml/syncreply/Submit',
encode_json(\%params),
{'Content-type' => 'application/json',
'Accept' => 'application/json'
}
);
它给出以下错误:
<ErrorCode>SerializationException</ErrorCode>
Error: System.Runtime.Serialization.SerializationException: There was an error deserializing the object of type Barabut.Gw.Submit. The data at the root level is invalid. Line 1, position 1. ---&gt; System.Xml.XmlException: The data at the root level is invalid. Line 1, position 1.
【问题讨论】:
-
使用哈希值,通过适当的模块序列化为 JSON(JSON::PP 在核心中)。
-
应该是
To => [ '90535xxxx' ],不过好像不是失败的原因。 -
@choroba,我意识到了这一点并将数组作为“To”参数发送。我看到我编码的 json 数据是正确的,但它仍然给出错误
-
错误提示系统尝试将数据解码为 XML,而不是 JSON。
-
@choroba 当我写正确的端点时它起作用了:)
标签: perl rest parameter-passing