The development of DataSnap is very easy to use, let’s not talk about other things, just go straight to the dry goods:
Create a DataSnap REST application
The code of ServerMethodsUnit1 is as follows:
unit ServerMethodsUnit1;
interface
uses
System.SysUtils, System.Classes, System.Json,
DataSnap.DSProviderDataModuleAdapter,
Datasnap.DSServer, Datasnap.DSAuth;
type
TServerMethods1 = class(TDSServerModule)
private
{ Private declarations }
public
{ Public declarations }
//Restful接口测试
//GET
function Test(Value: string): string;
//POST
function updateTest(Value: string; Obj: TJSONObject): string;
//DELETE
function cancelTest(Value: string): string;
//PUT
function acceptTest(Value: string; Obj: TJSONObject): string;
end;
implementation
{%CLASSGROUP 'System.Classes.TPersistent'}
{$R *.dfm}
uses
System.StrUtils, Data.DBXPlatform;
//GET
function TServerMethods1.Test(Value: string): string;
var
p1: string;
p2: string;
begin
p1 := GetInvocationMetadata.QueryParams.Values['p1'];
p2 := GetInvocationMetadata.QueryParams.Values['p2'];
Result := Format('Test called,Value:%s,p1:%s,p2:%s',[Value,p1,p2]);
end;
//POST
function TServerMethods1.updateTest(Value: string; Obj: TJSONObject): string;
var
p1: string;
p2: string;
data1: string;
data2: string;
begin
p1 := GetInvocationMetadata.QueryParams.Values['p1'];
p2 := GetInvocationMetadata.QueryParams.Values['p2'];
if Obj <> nil then
begin
data1 := obj.GetValue('data1').ToJSON;
data2 := obj.GetValue('data2').ToJSON;
end;
Result := Format('updateTest called,Value:%s,p1:%s,p2:%s,data1:%s,data2:%s',[Value,p1,p2,data1,data2]);
end;
//DELETE
function TServerMethods1.cancelTest(Value: string): string;
var
p1: string;
p2: string;
begin
p1 := GetInvocationMetadata.QueryParams.Values['p1'];
p2 := GetInvocationMetadata.QueryParams.Values['p2'];
Result := Format('cancelTest called,Value:%s,p1:%s,p2:%s',[Value,p1,p2]);
end;
//PUT
function TServerMethods1.acceptTest(Value: string; Obj: TJSONObject): string;
var
p1: string;
p2: string;
data1: string;
data2: string;
begin
p1 := GetInvocationMetadata.QueryParams.Values['p1'];
p2 := GetInvocationMetadata.QueryParams.Values['p2'];
if Obj <> nil then
begin
data1 := obj.GetValue('data1').ToJSON;
data2 := obj.GetValue('data2').ToJSON;
end;
Result := Format('acceptTest called,Value:%s,p1:%s,p2:%s,data1:%s,data2:%s',[Value,p1,p2,data1,data2]);
end;
end.
The client request is tested with the tools that come with Delphi (the source code is in Embarcadero\Studio\20.0\source\data\rest\restdebugger, you can learn how to submit a request).
GET call result:
POST call result:
PUT call result:
DELETE call result: