capture JSON object sent via POST in Mormot’s request payload

Asked

Viewed 801 times

0

I’m sent my object JSON via POST using the ExtJS 6.01. my application server was done in morMot in the Delphi Seattle. Sending in mode GET I usually take the parameters that come by url.

But sending via POST my object is placed in Request PayLoad how to retrieve this data sent via POST, using the morMot framework?

unit UnitJson;

interface
uses
  SynCommons, mORMot, mORMotHttpServer,mORMotReport, mORMotHttpClient,SynCrtSock,
  System.SysUtils,Vcl.Forms, Winapi.Windows, System.Classes,DB,Contnrs,XSBuiltIns,System.StrUtils;

 Type

  TMyService = class(TSQLRestServerFullMemory)

  published
    procedure myapp(Ctxt: TSQLRestServerURIContext);
  end;

  TMyHTTPServer = class(TSQLHttpServer)

  private
    oModel: TSQLModel;
    oSQLRestServerFullMemory: TMyService;
  public
    Model  : TSQLModel;
    function ServiceRegister(AImplementationClass: TInterfacedClass; const AInterfaces: PTypeInfo;const AContractExpected: RawUTF8): boolean;
    constructor Create(const APorta: RawUTF8; AThreadPoolCount: Integer); reintroduce;
    destructor Destroy; override;
  end;

 TServiceCustomAnswer = record
    Header: RawUTF8;
    Content: RawUTF8;
  end;

type
   TExtjsVO = class
  private
    fsuccess : boolean;
    frows     : TList;
    ftotal    : integer;
  published
    property success    : boolean            read Fsuccess write Fsuccess;
    property rows       : TList              read Frows    write Frows;
    property total      : integer            read Ftotal   write Ftotal;
  end;


// Class Return
  IMyInterface = interface(IInvokable)
  ['{E6C6ADFD-5977-4759-B400-AA4140F1D683}']
    function PostItem(params:RawUTF8):RawJSON;
  end;

  TMyClassDirect = class(TInterfacedObject, IMyInterface)

  public
    function PostItem(params:RawUTF8):RawJSON;
  end;

implementation

uses UnitServer;

{ TMyService }

procedure TMyService.myapp(Ctxt: TSQLRestServerURIContext);
var
  sFileName: TFileName;
begin
  sFileName := Ctxt.ResourceFileName;
  if sFileName = '' then
    Ctxt.Redirect('myapp/index.html')
  else
    Ctxt.ReturnFile(ExtractFilePath(Application.ExeName) + sFileName, true);
end;

{ TMyHTTPServer }

constructor TMyHTTPServer.Create(const APorta: RawUTF8; AThreadPoolCount: Integer);
begin
  oModel := TSQLModel.Create([], 'app');
  oSQLRestServerFullMemory := TMyService.Create(oModel, '', false, false);
  inherited Create(APorta, [oSQLRestServerFullMemory], '+', useHttpApiRegisteringURI, AThreadPoolCount);
end;

function TMyHTTPServer.ServiceRegister(AImplementationClass: TInterfacedClass; const AInterfaces: PTypeInfo; const AContractExpected: RawUTF8): boolean;
begin
  oSQLRestServerFullMemory.ServiceRegister(AImplementationClass,[AInterfaces], sicSingle).ContractExpected := AContractExpected;
  Result := true;
end;

destructor TMyHTTPServer.Destroy;
begin
  FreeAndNil(oModel);
  FreeAndNil(oSQLRestServerFullMemory);
  inherited;
end;

{ TMyClassDirect }

function TMyClassDirect.PostItem(params:RawUTF8): RawJSON;
begin
  // nao consigo pegar os dados via POST aqui..
  Result   := params;

end;

end.

1 answer

-1

I only used mormot as restfull and custom method I don’t know if it helps you but I’ll take the json in the call.inbody

procedure doSomething(Ctxt: TSQLRestServerURIContext);
var
  MyJson: String;
begin
  MyJson := Ctxt.Call.InBody;
  ...
end;

I hope I helped you with something

Browser other questions tagged

You are not signed in. Login or sign up in order to post.