Jump to content
HGRogers

Passing an array parameter to a REST server using Trestrequest

Recommended Posts

Hi

I'm going round in circles trying to pass an array parameter to a REST server

the required json structure of the params looks like this

 

{
  "payment_method": [
    "credit-card",
    "open-banking"
  ],
  "transaction_type": "SALE",
  "full_name": "Joe Blogs",
  "email":   etc etc

}

Using a TRESTRequest  adding those parameters with  name : value pairs  is easy.

For the array parameter I've tried the RESTRequest.Params.Options of poFlatArray or poListArray  with various combinations of double quotes commas curly and square braces without success

 

Any pointers as to how I add the required array parameter gratefully received.

 

The approach below I found on Stackoverflow looked promising  

but my Delphi (10.4) wont accept a string as the parameter to TJSONObject.ParseJSONValue()   ??

(That's despite the prototype in system.json having a version of ParseJsonValue that does take a string)

var aParam: TRESTRequestParameter;
begin
  aParam := RestReq.Params.AddItem(); //don't care about setting a name for it
  aParam.Value := TJSONObject.ParseJSONValue('{"payment_method":["credit-card","open-banking"]}');
  ......
  RestClient.Execute();
end;

 

Share this post


Link to post
type
  TSendParameter = class
  private
    Fpayment_method: TArray<string>;
    Ftransaction_type: string;
    Ffull_name: string;
    Femail: string;
  public
    property email: string read Femail write Femail;
    property full_name: string read Ffull_name write Ffull_name;
    property payment_method: TArray<string> read Fpayment_method write Fpayment_method;
    property transaction_type: string read Ftransaction_type write Ftransaction_type;
  end;
  
  ...
  
  var par := TSendParameter.Create;
  try
    par.payment_method := TArray<string>.Create('credit-card', 'open-banking');
    par.transaction_type := 'SALE';
    par.full_name := 'Joe Blogs';
    par.email := 'etc etc';
    RestReq.AddBody<TSendParameter>(par);
  finally
    par.Free;
  end;

 

Edited by Uwe Raabe
  • Like 1

Share this post


Link to post
1 hour ago, HGRogers said:

The approach below I found on Stackoverflow looked promising  

but my Delphi (10.4) wont accept a string as the parameter to TJSONObject.ParseJSONValue()   ??

(That's despite the prototype in system.json having a version of ParseJsonValue that does take a string)

Are you sure that is the real error?  ParseJSONValue() does indeed have an overload which takes in a string.  However, the TRESTRequestParameter.Value property is also a string, but ParseJSONValue() returns a TJSONValue object pointer.  So you would need to either just get rid of the TJSONObject entirely:

var aParam: TRESTRequestParameter;
begin
  aParam := RestReq.Params.AddItem(); //don't care about setting a name for it
  aParam.Value := '{"payment_method":["credit-card","open-banking"]}';
  ......
  RestClient.Execute();
end;

Or, convert it to a string:

var
  aParam: TRESTRequestParameter;
  aObj: TJSONObject;
begin
  aObj := TJSONObject.ParseJSONObject('{"payment_method":["credit-card","open-banking"]}') as TJSONObject;
  try
    aParam := RestReq.Params.AddItem(); //don't care about setting a name for it
    aParam.Value := aObj.ToJson;
  finally
    aObj.Free;
  end;
  ......
  RestClient.Execute();
end;

 

Share this post


Link to post

Thank you Remy - I've got both approaches working - But As soon as I supply enough parameters to satisfy the API I'm getting a '500 Internal Server Error' code. So not sure where to go from here. Using an echo server rather than the real endpoint I see all the JSON appears well formed and I'm assured by the Server developers that the calls made in their sandbox and live app (which can be tried live via their developers site) are identical to the calls I'm making with Delphi. 

Share this post


Link to post

SOLVED

Should anyone embark upon using the blink payment api.

When using TRESTRequest

The Header parameter used for all calls after a Token is obtained is called 'Authorization' not Authentication as shown at 

       https://api-docs.blinkpayment.co.uk/apidocs/tokens

and it requires poDoNotEncode

e.g. req.Params[0].Value := 'Bearer ' + theAccessToken ;

 

Capture.PNG

Edited by HGRogers
missing url

Share this post


Link to post

A quick look into REST.Consts.pas reveals:

const
  HTTP_HEADERFIELD_AUTH = 'Authorization'; // do not localize

It is used by the TCustomAuthenticator components, which a TRESTClient can be linked to via its Authenticator property. Especially TOAuth2Authenticator comes to mind with TokenType set to ttBEARER and AccessToken set to the proper value.

Share this post


Link to post

I know we are now rather off topic but thanks for the additional insight.

All the best

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×