1

This might be a simple one.

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');

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]"
}

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.

9
  • I have absolutely no knowledge of REST or JSON, so only a comment, but did you try: RestReq.Params.AddItem('["v1", "v2", v3"]');? It is the first thing I would try. Commented Jul 29, 2014 at 18:58
  • Yes, and the server receives: Commented Jul 29, 2014 at 20:10
  • "[\"v1",\"v2",\"v3"]". Commented Jul 29, 2014 at 20:11
  • Hmmm... OK. I have no better idea, sorry. Commented Jul 29, 2014 at 22:19
  • What if you set the Options for the parameter to poDoNotEncode? Commented Jul 29, 2014 at 23:21

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

In my case with XE7 I must write aParam.Value := '{"param1":"value1","param2":"value2","param3":["v1","v2","v3"]}';
In my case with Tokyo 10.2, I used aParam.Value := TJSONObject.ParseJSONValue('{"a":"b"}').ToJSON;
0

While looking for this in releation to some public API I ended up here so to help anyone with a similar issue there's a way to encode arrays into a query string although the documentation (as usual) is less than easy to find.

e.g.

uses
  REST.Client, REST.Types;

...

  RESTRequest := TRESTRequest.Create(Nil);

  RESTParams := TRESTRequestParameterList.Create(Nil);
  // Add any normal parameters e.g.
  RESTParams.AddItem('output','json'); // etc - any easy ones
  // And here's an array for fl[] with multiple values
  RESTParams.AddItem('fl','avg_rating;creator;date', pkGETorPOST, [poPHPArray]);

  RESTRequest.Params := RESTParams;

  // Add other stuff now the params are correctly set

See TRESTRequestParameterOption for other variants instead of poPHPArray

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.