Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: How to use RestAPI Put with Array parameters?


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 3 - Last Post: Oct 17, 2016 11:58 AM Last Post By: Robert Triest
László Mlnvszky

Posts: 106
Registered: 10/21/09
How to use RestAPI Put with Array parameters?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 17, 2016 7:14 AM
Hi!
I need a little help communicating with a RESTAPI, updating product stock with PUT method.
The API itself works with the RESTRequest components with GET and even with PUT methods.

But when I need to add an Array as a parameter, it fails.
Any idea how to do it correctly?

RESTClient1.Authenticator := HTTPBasicAuthenticator1 ;
HTTPBasicAuthenticator1.Username := shopapi_user ;
HTTPBasicAuthenticator1.Password := shopapi_password;
LJSONObject:= TJSONObject.Create;
LJSONObject.AddPair('sku','400102');
LJSONObject.AddPair('stock1','2');
RESTRequest1.AddBody( LJSONObject );
RESTRequest1.Method := TRESTRequestMethod.rmput ;
RESTRequest1.Execute;

I get an error:
"error":415,
"message":"The server is refusing to service the request
because
the entity of the request is in a format not supported by the
requested resource for the requested method"

THE API documentation says:
Url: /products
Request

Array
(
[sku] => SKU-11
[modelNumber] => 0
[orderable] => 1
[price] => 139900.0000
[multiplier] => 1.0000
[multiplierLock] => 0
[loyaltyPoints] =>
[stock1] => 0

)

Response
HTTP STATUS CODE: 200 vagy 201

In PHP, I got an example:
$response = $apiCall->execute('POST', $url, array('sku' => 'something', 'price' => 1000));

How to do it in Delphi? with JSON?
(I am using Berlin 10.1 update1)
Robert Triest

Posts: 687
Registered: 3/24/05
Re: How to use RestAPI Put with Array parameters?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 17, 2016 7:59 AM   in response to: László Mlnvszky in response to: László Mlnvszky
Try something like this..

var jsobj, jso: TJsonObject;
     jsa: TJsonArray;
     jsp: TJsonPair;
Begin   
    jsobj := TJsonObject.Create();
    try
      jsa := TJsonArray.Create();
      jsp := TJsonPair.Create('Array', jsa);
      jsobj.AddPair(jsp);
 
      jso := TJsonObject.Create();
      jso.AddPair(TJsonPair.Create('sku', '400102'));
      jso.AddPair(TJsonPair.Create('stock1','2'));
      jsa.AddElement(jso);
 
      RESTRequest1.AddBody(jsobj.ToString);
    finally
      jsobj.Free;
    end;
end;
László Mlnvszky

Posts: 106
Registered: 10/21/09
Re: How to use RestAPI Put with Array parameters?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 17, 2016 8:17 AM   in response to: Robert Triest in response to: Robert Triest
Robert Triest wrote:
Try something like this..

var jsobj, jso: TJsonObject;
     jsa: TJsonArray;
     jsp: TJsonPair;
Begin   
    jsobj := TJsonObject.Create();
    try
      jsa := TJsonArray.Create();
      jsp := TJsonPair.Create('Array', jsa);
      jsobj.AddPair(jsp);
 
      jso := TJsonObject.Create();
      jso.AddPair(TJsonPair.Create('sku', '400102'));
      jso.AddPair(TJsonPair.Create('stock1','2'));
      jsa.AddElement(jso);
 
      RESTRequest1.AddBody(jsobj.ToString);
    finally
      jsobj.Free;
    end;
end;

Interesting Approach, thank you, sounds really good, but I got an error 400 Bad Request:
{
"error":40001,
"message":"Data parameter not found in POST/PUT!"
}
Robert Triest

Posts: 687
Registered: 3/24/05
Re: How to use RestAPI Put with Array parameters?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 17, 2016 11:40 AM   in response to: László Mlnvszky in response to: László Mlnvszky
RESTRequest1.AddBody(jsobj.ToString);
Interesting Approach, thank you, sounds really good, but I got an error 400 Bad Request:

Watch jsobj.ToString and see if it is compatible with the server JSon format.

Use https://jsonformatter.curiousconcept.com/ and see if the format is a JSON format.
You can not force Delphi JSON objects to produce a non-JSON format..
The format is very simple, so maybe create your own TStrings object..

Edited by: Robert Triest on Oct 17, 2016 8:43 PM

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02