#1 2021-11-20 06:57:43

srp
Member
Registered: 2014-09-02
Posts: 11

Loading & Creating from a TRestHttpClient Definition file ?

I  am trying to create a TRestHttpClient from a Json File.

However, when I run the system raises an error "Winhttp.dll Error 57". Is there something I need to initiate after loading from a file?

File test.json created using the following code
   Model := CreateSampleModel;
   HttpClient := TRestHttpClient.Create('localhost', HttpPort, Model);
   HttpClient.DefinitionToFile('c:\test.json');


Later load this using   
   Model := CreateSampleModel;
   HttpClient := TRestHttpClient(TRestHttpClient.CreateFromFile(Model,'c:\test.json',true));
   HttpClient.ServerTimestampSynchronize;  // Error  at this point.


I am using the latest version of Mormot2.


Thank You

Last edited by srp (2021-11-20 06:58:13)

Offline

#2 2021-11-22 09:30:51

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

Re: Loading & Creating from a TRestHttpClient Definition file ?

Please try to debug a bit more and find out what is not working.
Perhaps some information is lacking in the json definition file.

Why did you write HttpClient := TRestHttpClient(...) ?
It is not a good programming practice to force the type.
Writing HttpClient := .... as TRestHttpClient is safer.

Offline

#3 2021-11-22 13:42:47

srp
Member
Registered: 2014-09-02
Posts: 11

Re: Loading & Creating from a TRestHttpClient Definition file ?

Thank You @Ab for the reply. 

I  tried hard for hours to find out the exact reason for this issue, but my knowledge is limited, and could not find the actual reason for this issue.

But I assume it is something that is related to the initialization of the Object from JSON.

It can be easily reproduced on a mormot server using this code. (I am trying the Example of ex\ThirdPartyDemos\martin-doyle\04-InterfacedBasedServices\src)

FYI : using Delphi 10.3 community Edition

procedure TForm1.Button1Click(Sender: TObject);
var
  sTemp : RawUtf8;
begin
 Model := TOrmModel.Create([],'root');
 HttpClient := TRestHttpClient.Create('localhost', '11111',Model,false);
 HttpClient.ServerTimestampSynchronize;
 sTemp:= HttpClient.DefinitionToJson;

 if Assigned(Model) then FreeAndNil(Model);
 if Assigned(HttpClient) then FreeAndNil(HttpClient);

 Model := TOrmModel.Create([],'root');
 httpClient:=   TRestHttpClient.CreateFromJson(Model,sTemp,false) as TRestHttpClient ;
  HttpClient.ServerTimestampSynchronize;    // ERROR at this point
 if Assigned(Model) then FreeAndNil(Model);
 if Assigned(HttpClient) then FreeAndNil(HttpClient);
End;

Thank You for pointing out and surely will follow good programming practice.  Any good link\articles\Books that you can suggest for good Delphi Programming practices that you follow.

Thank You for your time and effort.

Last edited by srp (2021-11-23 07:59:20)

Offline

#4 2021-11-23 21:06:13

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 392

Re: Loading & Creating from a TRestHttpClient Definition file ?

I can confirm this error using Mormot2

The error happens in mormot.net.client, line 2383 but this is caused by the previous call in line 2375 due the contents of fProxyName and fProxyByPass that have a content of '='
  fSession := WinHttpApi.Open(
    pointer(Utf8ToSynUnicode(fExtendedOptions.UserAgent)),
    access,
    pointer(Utf8ToSynUnicode(fProxyName)),
    pointer(Utf8ToSynUnicode(fProxyByPass)), 0);

From mormot.rest.http.client line 652,
fProxyName and fProxyByPass have a content of '='

where the initial JSON is:
'{"Kind":"TRestHttpClientWinHttp","ServerName":"localhost:11111","DatabaseName":"IgnoreSSLCertificateErrors=0&ConnectTimeout=30000&SendTimeout=30000&ReceiveTimeout=30000&ProxyName=&ProxyByPass=","User":"","Password":""}'
I think the problem is that ProxyName ProxyByPass are empty after the '=' for UrlDecodeValue

Last edited by dcoun (2021-11-23 21:16:06)

Offline

#5 2021-11-23 21:33:44

esmondb
Member
From: London
Registered: 2010-07-20
Posts: 299

Re: Loading & Creating from a TRestHttpClient Definition file ?

Re: 'Any good link\articles\Books' the 1970's book by Niklaus Wirth 'Algorithms+Data Structures=Programs' is still worth a read.

Last edited by esmondb (2021-11-23 21:34:55)

Offline

#6 2021-11-24 03:56:28

srp
Member
Registered: 2014-09-02
Posts: 11

Re: Loading & Creating from a TRestHttpClient Definition file ?

Thank You @dcoun for confirming.
Thank You @esmondb.  Will definitely go through the book

I further investigated and think the error is at this point  'mormot.rest.core' at line 1934.

constructor TRest.RegisteredClassCreateFrom(aModel: TOrmModel;
  aDefinition: TSynConnectionDefinition; aServerHandleAuthentication: boolean);
begin
  Create(aModel);
  // ADefinition  should be used / assigned at this point.
end;


Edit :  I am wrong.

@dcoun you are spot on. I checked and  if I make  the change in line  2368  of mormot.net.client from

  if fProxyName = '' then

to

if (fProxyName = '')  or (fProxyName = '=''''') then

.

It works as expected.

Thank You

Last edited by srp (2021-11-24 06:32:44)

Offline

#7 2021-11-24 17:55:00

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

Re: Loading & Creating from a TRestHttpClient Definition file ?

The JSON seems fine:

{"Kind":"TRestHttpClientWinHttp","ServerName":"localhost:11111","DatabaseName":"IgnoreSSLCertificateErrors=0&ConnectTimeout=30000&SendTimeout=30000&ReceiveTimeout=30000&ProxyName=&ProxyByPass=","User":"","Password":""}

The problem seems with TRestHttpClient.CreateFromJson, which fails to unserialize the values from DatabaseName. After unserialization, ProxyName should be a void '' string.

There was indeed a bug in TRestHttpClientGeneric.RegisteredClassCreateFrom.
Which was also existing in mORMot 1.18 - I will correct it there too!

Sorry for the issue.

Offline

#8 2021-11-24 19:10:26

srp
Member
Registered: 2014-09-02
Posts: 11

Re: Loading & Creating from a TRestHttpClient Definition file ?

Thank You @Ab.

Offline

Board footer

Powered by FluxBB