德尔福TRestRequest数组参数 [英] Delphi TRestRequest array parameter
问题描述
这可能是一个简单的。
我使用RESTClient实现组件访问RESTful服务与德尔福XE6:TRestClient,TRestRequest,TRestResponse和THTTPBasicAuthenticator
I'm accessing a RESTFul service with Delphi XE6 using RestClient components: TRestClient, TRestRequest, TRestResponse and THTTPBasicAuthenticator.
该服务需要,我有没有问题,添加参数:
The service requires parameters which I have no problems adding:
RestReq.Params.AddItem('param1', 'value1');
RestReq.Params.AddItem('param2', 'value2');
通过在服务器端上code,它看起来像:
With the above code on the server side it looks like:
{
"param1":"value1",
"param2":"value2"
}
然而,当我需要发送一个参数,它是一个数组,我尝试:
However, when I need to send a parameter which is an array and I try:
RestReq.Params.AddItem('param1', 'value1');
RestReq.Params.AddItem('param2', 'value2');
RestReq.Params.AddItem('param3', '[v1, v2, v3]');
,因为第三个参数不是预期的阵列的服务将拒绝它。这是正确的,因为它接收:
The service will reject it because the third parameter is not the expected array. Which is correct because it receives:
{
"param1":"value1",
"param2":"value2",
"param3":"[v1,v2,v3]"
}
我知道这看起来很简单。皆已RestClient.ContentType,试图操纵数组。试图改变参数的ContentType,选项和猜测的解决方案是不是一个游戏,我喜欢玩。
所以问题是:使用RESTClient实现组件¿我怎么能叫我的服务具有以下参数
I know it looks very simple. Have switched RestClient.ContentType, have tried to manipulate the array. Have tried changing the parameter ContentType, Options and guessing the solution is not a game I like to play. So the question would be: Using the RestClient components ¿how can I call my service with the following parameters?
{
"param1":"value1",
"param2":"value2",
"param3":[
"v1",
"v2",
"v3"
]
}
在前进,感谢您的时间。
In advance, thanks for your time.
推荐答案
完成!它看起来像我是做了错误的(或复杂)的方式。该服务期待一个JSON对象,我被物业构建它财产。还有一个更简单的方法:
Done! It looks like I was doing it the wrong (or complicated) way. The service was expecting a JSON object and I was building it property by property. There is an easier way:
var aParam: TRESTRequestParameter;
begin
RestReq.Method := rmPOST; {or rmGET, ...}
aParam := RestReq.Params.AddItem(); //don't care about setting a name for it
aParam.Value := TJSONObject.ParseJSONValue('{"param1":"value1","param2":"value2","param3":["v1","v2","v3"]}');
aParam.ContentType := ctAPPLICATION_JSON;
//set proxy params, resource, etc.
RestClient.Execute();
end;
和会做!感谢所有为您的意见。
And that will do! Thanks all for your comments.
这篇关于德尔福TRestRequest数组参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!