Post json with RESTRequest

Need help with RESTRequest
Is there any example of doing RESTRequest post with JSON ?

I try to use param like below, but I can't put my json array in the parameter.

 procedure TForm1.Button2Click(Sender: TObject);
var
  jo: TJSONObject;
  ja: TJSONArray;
  i: integer;
begin

  ja := TJSONArray.Create;
  jo := TJSONObject.Create;
  jo.AddPair('First_Name', Edit1.Text);
  jo.AddPair('Last_Name', Edit2.Text);

  ja.AddElement(jo);


  //RESTRequest2.Params.AddItem('jsondata', ja, TRESTRequestParameterKind.pkGETorPOST);
  RESTRequest2.Execute;

  Memo2.Lines.Text:=RestResponse2.Content;
end;


Below is my server method

function TServerMethods1.InsertRecord(Data:TJSONArray): string;
var
  i: Integer;
  total :integer;
begin

  try
    Data.Size;
    fdqryStaff.Close;
    fdqryStaff.Open;
    total:=fdqryStaff.RecordCount;


    fdqryStaff.Insert;
    fdqryStaffTrxKey.Value:=total+1;
    fdqryStaffFirst_Name.Value:=TJSONObject(Data.Get(0)).Get('First_Name').JsonValue.Value;
    fdqryStaffLast_Name.Value:=TJSONObject(Data.Get(0)).Get('Last_Name').JsonValue.Value;
    fdqryStaff.Post;

    Result := 'Success';


  except
    on E : Exception do
      Result:= E.ClassName+' error raised, with message : '+E.Message;
  end;

end;
0
Fuandi
3/5/2014 2:46:52 AM
embarcadero.delphi.firemonkey 4901 articles. 4 followers. Follow

5 Replies
13186 Views

Similar Articles

[PageSpeed] 8

Use:

RESTRequest2.Method := TRESTRequestMethod.rmPOST;

RESTRequest2.AddParameter('jsondata', ja);

You don't want to do a GET if your JSON is really big. You could also Base64 encode your JSON or if you have data like a PNG you can Base64 encode that on the client and then decode it on the server.


This covers a lot of the REST client:
http://docwiki.embarcadero.com/RADStudio/XE5/en/REST_Client_Library

Also if you have Delphi on the server side as well you might try DataSnap. It handles the JSON for you.
http://docwiki.embarcadero.com/RADStudio/XE5/en/DataSnap_Overview_and_Architecture
0
Eli
3/5/2014 4:54:43 AM
I got this error

[DCC Error] Unit1.pas(84): E2250 There is no overloaded version of 'AddParameter' that can be called with these arguments


There is no jsonarray parameter for this addparameter method, it can only accept string

http://docwiki.embarcadero.com/Libraries/XE5/en/REST.Client.TCustomRESTRequest.AddParameter
0
Fuandi
3/6/2014 1:42:22 AM
RESTRequest2.AddParameter('jsondata', ja.ToString);
0
Eli
3/6/2014 2:17:14 AM
is there a way to post json/xml instead of string ? 
If I pass it as string, then my server need to read the string and convert it into json/xml. Not a good way to do.
0
Fuandi
3/6/2014 7:17:13 AM
This may be what you want. It talks about using multipart/form-data and multipart/mixed MIME types for POST:
https://stackoverflow.com/questions/4007969/application-x-www-form-urlencoded-or-multipart-form-data

This shows you how to construct a custom POST request for use with TIdHTTP.
https://stackoverflow.com/questions/20092510/support-for-utf-8-encoded-string-in-trestclient-delphi-xe5

You can also use TRESTRequest.AddBody() to add a Stream.
http://docwiki.embarcadero.com/Libraries/XE5/en/REST.Client.TCustomRESTRequest.AddBody
0
Eli
3/6/2014 2:27:22 PM
Reply:

Web resources about - Post json with RESTRequest - embarcadero.delphi.firemonkey

How to start receiving inbound email — Mailgun REST API 2.0 documentation
public static ClientResponse CreateRoute () { Client client = Client . create (); client . addFilter ( new HTTPBasicAuthFilter ( "api" , "YOUR_API_KEY" ...

Call your CRM Platform! Using an ASP.NET Web API to Link Twilio and Salesforce.com
I love mashups. It’s fun to combine technologies in unexpected ways. So when Wade Wegner of Salesforce asked me to participate in a webinar about ...

Resources last updated: 2/16/2016 1:18:18 PM