简体   繁体   中英

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]); 
  1. 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 ?

  2. 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.

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.

As @erwin Mentioned you should check the DataSnap REST Client Module. At least that's how I have set up my Proof of Concept application I made for a client. For example the DataSnap Server has a few REST Client Module which has a public method called GetListByFetchParams which returns a TFDJSONDataSets. The implementation looks a bit like this :

function TBaseServerMethods.InternalGetListByFetchParams(aFetchParams: TFetchParams): TFDJSONDataSets;
begin
  {$IFDEF USE_CODESITE}
  CSServerMethods.TraceMethod( Self, 'InternalGetListByFetchParams', tmoTiming );
  CSServerMethods.Send( csmFetchParams, 'aFetchParams', aFetchParams );
  {$ENDIF}

  Result := TFDJSONDataSets.Create;

  // Close the ListDataSet
  ListDataSet.Close;

  // Open the List DataSet
  ListDataSet.Open;

  // Add the ListDataSet to the result using a predefined name.
  TFDJSONDataSetsWriter.ListAdd( Result, ListDataSetName, ListDataSet );

  {$IFDEF USE_CODESITE}
  CSServerMethods.Send( 'qryList.SQL', ListDataSet.SQL );
  CSServerMethods.Send( csmFDDataSet, 'qryList DataSet Contents', ListDataSet );
  {$ENDIF}
end;

The most important thing is that I create a TFDJSONDataSets which I set up as th result. I removed some code but in short the aFetchParams is used to alter the SQL Statement of the FireDaC Query component which is referenced by ListDataSet and it then Opens that dataset. Next I use a TFDJSONDataSetsWriter.ListAdd call to add our ListDataSet to the TFDJSONDataSets which is the result of the function.

Client Side I need some code too. This code will call the remote method on the DataSnapServer which will return the TFDJSONDataSets and which I can then deserialise into the TFDMemTables on the client :

procedure TBaseAgent.FetchListWithParams(aFetchParams : TFetchParams);
var
  oListData      : TFDJSONDataSets;
  oKeyValues     : TDictionary< String, String >;
begin
  {$IFDEF USE_CODESITE}CSAgent.TraceMethod( Self, 'FetchListWithParams', tmoTiming );{$ENDIF}

  oListData      := FetchListWithParamsFromServer( aFetchParams );
  try
    tblList.AppendData( TFDJSONDataSetsReader.GetListValue( oListData, 0 ) );
    tblList.Open;
  finally
    FreeAndNil( oListData );
  end;
end;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM