#1 2019-02-03 21:59:24

larand54
Member
Registered: 2018-12-25
Posts: 104

Using mORMot to create a client connecting to an existing webservice

We want to create an application calling an existing web service with https.
The first service call will be authentication, parameters are username and password.
On success, we will get a token that is saved to be used in future calls of other service functions.
Responses will be in JSON.

I'm new to mORMot and not yet especially familiar with all functions and need some help finding the key functions.
I've been searching for a while but not found any that sound's to fit in. I'm sure there is but would be grateful to any help.
Looked at the sample code of "Project06ClientMain" and found "Client := TSQLRestClientURINamedPipe.Create(Model,'RestService');". But this uses NamedPipes and I need https.
Also, it uses "model" and wonder if it necessary to create a TSQLRecord here or if there is possible to pick the items from JSON directly here?

I've made it work already with some Delphi components but are very interested in mORMot and want to use it as much as possible.

Last edited by larand54 (2019-02-04 09:26:10)


Delphi-11, WIN10

Offline

#2 2019-02-03 23:14:14

EMartin
Member
From: Buenos Aires - Argentina
Registered: 2013-01-09
Posts: 337

Re: Using mORMot to create a client connecting to an existing webservice

I use TWinHttp that is in unit SynCrtSock.pas. I did make a consumer webservice for SOAP and JSON with SSL support.


Esteban

Offline

#3 2019-02-06 18:01:34

larand54
Member
Registered: 2018-12-25
Posts: 104

Re: Using mORMot to create a client connecting to an existing webservice

I've been very busy this week will still be for a while.  After all, I tried to test your suggestion, but I can't make it work. I have no idea how to use this.

I tried this but it won't do a thing.

  cl := TWinHttp.Create('localhost', '4430', false);
  authResponse := cl.Post('localhost/authenticate/',AuthData,'Key: Content-Type, Value: application/json',true,HDRResponse);

I tried to begin without using https and also on a localhost server to minimize error sources.

I debugged the code and it seems that cl.post won't care about what I did in create.

Any idea of what I missing?


Delphi-11, WIN10

Offline

#4 2019-02-07 14:58:27

xalo
Member
Registered: 2016-09-22
Posts: 32

Re: Using mORMot to create a client connecting to an existing webservice

I think there are some mistakes in your code. Please, see below a https authorization example of the Asana API:

    http := TWinHttp.Create('app.asana.com','443',true);
    try
      Status := http.Request('/api/1.0/tasks',
                             'POST',0,
                             FormatUTF8('Authorization: Bearer %',[fPersonalAccessToken]), 
                             VariantSaveJson(Json),
                             '',ResHeader,ResData);
    finally
      http.Free;
    end;

In case to send a POST request to a http webservice:

    http := TWinHttp.Create(HostName,'8888',false);
    try
      Status := http.Request(FormatUTF8('/things/link/%',[ThingCode]),
                             'POST',0,
                             'Content-Type: application/json'+#10+'Accept: application/json', 
                             json,
                             '',ResHeader,ResData);
    finally
      http.Free;
    end;

 
Please read the incredible documentation to get further details.

Offline

#5 2019-02-11 07:32:34

larand54
Member
Registered: 2018-12-25
Posts: 104

Re: Using mORMot to create a client connecting to an existing webservice

That made it! big_smile
Thanks a lot, both of you.
Now I can get rid of the Indy components that require a lot of bugfixes, I had to do myself, to make https work.
Thanks again.


Delphi-11, WIN10

Offline

Board footer

Powered by FluxBB