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 |
![]() |
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 |
![]() |
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 |
![]() |
RESTRequest2.AddParameter('jsondata', ja.ToString);
![]() |
0 |
![]() |
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 |
![]() |
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 |
![]() |