Passing a JSON object to Datasnap Server application

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Passing a JSON object to Datasnap Server application



I have a datasnap application server and a client witten in Delphi Tokyo 10.2. I need to know whether I do the communication between the two correctly. I will write down the client code here :



Client Code:


procedure TRemoteAcessModule.InitialiseRESTComponents(BaseURL: string);
var
ReqParam : TRESTRequestParameter;
begin
//URL to which client has to connect
RESTClient1.BaseURL := BaseURL;//'http://192.168.110.160:8080/datasnap/rest/TserverMethods1';
RESTClient1.Accept:= 'application/json, text/plain; q=0.9, text/html;q=0.8,';
RESTClient1.AcceptCharset := 'UTF-8, *;q=0.8';

RESTRequest1.Client := RESTClient1;
RESTRequest1.Response := RESTResponse1;

RESTResponse1.RootElement := 'Rows';
RESTResponseDataSetAdapter1.Response := RESTResponse1;
RESTResponseDataSetAdapter1.Dataset := FDMemTable1;
RESTResponseDataSetAdapter1.NestedElements := true;

end;

class function TItemsHelper.InsertItem(item: TItem): boolean;
var
ds : TDataset;
begin
ds := RemoteAcessModule.CallResource2('InsertItem', TJson.ObjectToJsonString(item));
if ds.Fields[0].AsInteger > 0 then
result := true
else
result := false
end;

function TRemoteAcessModule.CallResource2(ResourceName: string): TDataset;
begin
CallResourceNoParams(ResourceName);
result := GetQueryResult;
end;

procedure TRemoteAcessModule.CallResource(ResourceName, Params: string);
begin
RESTResponseDataSetAdapter1.Dataset := TFDMemTable.Create(self); //new
RESTRequest1.Resource := ResourceName;
RESTRequest1.ResourceSuffix := '{qry}';
RESTRequest1.AddParameter('qry', TIdURi.ParamsEncode(Params), TRESTRequestParameterKind.pkURLSEGMENT, [poAutoCreated]);
RESTRequest1.Execute;
RESTResponseDataSetAdapter1.Active := true;
RESTRequest1.Params.Delete('qry');
RESTRequest1.ResourceSuffix :='';
end;



At server side, I have written a function which decodes the json and inserts the item data into the database.



So, to insert an item, i have call TItemsHelper.InsertItem by passing the Titem object i need to insert. This works. But the doubts I have are :


RESTRequest1.AddParameter('qry', TIdURi.ParamsEncode(Params), TRESTRequestParameterKind.pkURLSEGMENT, [poAutoCreated]);



is the way mentioned above the correct method to pass the encoded
json object to server (making it a part of the URL ) ? If not how
can i send json data to datasnap server ?



Can I get some advise on things I have done incorrectly here ?



Thanks in advance for your patience to go through this. Looking forward for some guidance.




1 Answer
1



If you have a DataSnap REST Server, there is a wizard to build DataSnap client application. The REST Client Library you use is for generic REST server. If you have DataSnap Server, and you want Delphi client application, you can take advantage to use wizard DataSnap REST Client Module.



BTW, Embarcadero now recommends RAD Server to develop REST server.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

Visual Studio Code: How to configure includePath for better IntelliSense results