1

I have the following problem

I need to make a POST request and it requires an array of objects to be passed in one of the parameters.

My question is this: how can I do this?

To add a simple parameter, I use TRESTRequest.AddParameter('api_token', xxxxxx ) for example and it works perfectly

How could I pass an array to request if AddParameter doesn't let me use it that way?

everton
  • 11
  • 1
  • Please show an example of JSON you want to get. And show the code you have so far. – fpiette Jul 18 '21 at 05:59
  • The value of a parameter is a string. HTTP has no concept of "array in a parameter". – Olivier Jul 18 '21 at 07:07
  • @Olivier The OP has tagged is question with JSON, so I assumed the parameter is a JSON formatted string which has to contain an array of objects. – fpiette Jul 18 '21 at 07:27
  • But in my opinion, the actual question is not about how should be the request. The missing information here is, how the REST Server requires the request to pass this array of objects. Test first in a Postman tool and please put the results here to help us provide you an accurated answer. – Xalo Jul 19 '21 at 07:53

1 Answers1

0

I chased this for a few days recently. I finally installed Fiddler on Windows to see what TRESTRequest was doing with my parameters. After that it was fairly easy to find the essential clue on How to pass an array within a query string -- that the correct syntax depends on the server-side implementation.

For the Stripe API, it worked to add suffix '[]' and to add each array element individually.

    // assuming these variable types exist already
    rr: TRESTRequest;
    i: Integer;
    Stripe_Payment_Method_Types: array of string;


    // here is the loop to pass the array of strings
    for i := 0 to High(Stripe_Payment_Method_Types) do
    begin
      // the [] is required to cause Stripe to see it as an array of strings
      rr.Params.AddItem('payment_method_types[]',  // <!-- trailing []
            Stripe_Payment_Method_Types[i]);
    end;