#1 2024-07-28 11:44:06

JD
Member
Registered: 2015-08-20
Posts: 118

Converting sample 04 HTTP Client-Server to mORMot2

Hi there everyone,

I'm trying to convert the 04 HTTP Client-Server sample from mORMot 1 to mORMot2. Everything has gone well so for except for the Project04Client.lpr file which did not compile.

I finally got it to compile in the form below:

program Project04Client;

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Interfaces, // this includes the LCL widgetset
  Forms,
  SysUtils,
  mormot.rest.http.client, 		// For TRestHttpClient
  mormot.orm.rest,        		// For TRestOrm (note: TRest replaced TSQLRest and is in the mormot.rest.core unit)
  //mORMot,
  //mORMotHttpClient,
  Unit1 in '..\01 - In Memory ORM\Unit1.pas' {Form1},
  SampleData in '..\01 - In Memory ORM\SampleData.pas';

{$R *.res}

var Server: AnsiString;
begin
  RequireDerivedFormResource := True;
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Form1.Caption := ' Sample 04 - HTTP Client';
  if ParamCount=0 then
    Server := 'localhost' else
    Server := AnsiString(Paramstr(1));

  TRestHttpClient(Form1.Database) := TRestHttpClient.Create(Server,'8080',Form1.Model);  // <<<< Problem here
  TRestHttpClient(Form1.Database).SetUser('User','synopse');                              // <<<< Problem here

  //Form1.Database := TSQLHttpClient.Create(Server,'8080',Form1.Model);
  //TSQLHttpClient(Form1.Database).SetUser('User','synopse');
  Application.Run;
end.  

However when I run it and try to add data, I get Access Violation errors.

The Lazarus code editor tells me that there is a problem on these lines even though the program compiled.

  TRestHttpClient(Form1.Database) := TRestHttpClient.Create(Server,'8080',Form1.Model);
  TRestHttpClient(Form1.Database).SetUser('User','synopse');

It places a warning icon on the lines and the warning says Warning: class types "TRestOrm" and "TRestHttpClientSocket" are not related.

In the mORMot 1 version, the "Database" variable was of the type "TSQLRest" but in this mORMot 2 conversion, I replaced "TSQLRest" with "TRest" and even "TRestOrm" but I still get the same access violation issue.

Can anyone help me get around this problem?

Thanks a lot,

JD

Last edited by JD (2024-07-28 11:45:36)

Offline

#2 2024-07-28 18:40:41

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,655
Website

Re: Converting sample 04 HTTP Client-Server to mORMot2

1) In mORMot 2, we put the ORM classes distinct from the main REST classes.
You need to use an IRestOrm interface, from TRest.Orm.

2) Never ever use forced typecast, use (Form1.Database as TRestHttpClient)

Offline

Board footer

Powered by FluxBB