Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2023-01

TFPHttpClient & TFPHttpServer

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

duilio foschi

I would like to write some code where a TFPHttpClient component sends a minimal json payload to a TFPHttpServer that (reads the payload).

The working of the TFPHttpClient component is clear enough, while its counterpart TFPHttpServer proves rather obscure to me.

TFPHttpClient
===========
Page https://wiki.freepascal.org/fphttpclient
contains complete and clear code:

var
  Client: TFPHttpClient;
  Response: TStringStream;
  Params: string = '{"Name": "John", "Age": 32}';
begin
  Client := TFPHttpClient.Create(nil);
  Client.AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)');
  Client.AddHeader('Content-Type', 'application/json; charset=UTF-8');
  Client.AddHeader('Accept', 'application/json');
  Client.AllowRedirect := true;
  Client.UserName := USER_STRING;
  Client.Password := PASSW_STRING;
  Client.RequestBody := TRawByteStringStream.Create(Params);
  Response := TStringStream.Create('');
  try
    try
      Client.Post(TheURL, Response);
      Writeln('Response Code: ' +
IntToStr(Client.ResponseStatusCode)); // better be 200
    except on E: Exception do
      Writeln('Something bad happened: ' + E.Message);
    end;
  finally
    Client.RequestBody.Free;
    Client.Free;
    Response.Free;
  end;

I reckon I need to set - say-
TheURL:='http://localhost:9200';
here

TFPHTTPServer
=============

say we use
TTestHTTPServer=class(TFPHTTPServer);
for our server,

I understand that all action takes place inside procedure DataModuleRequest (so far so good):

procedure TTestHTTPServer.DataModuleRequest(Sender: TObject; ARequest: TRequest;
 AResponse: TResponse; var Handled: Boolean);
begin
   //
end;

Q1: how can I create an object of type jsondata from ARequest?

Q2: can I get a UTF8 string from ARequest?

Q3: TFPHTTPServer has property Port. I reckon it will be set to 9200 here. Is it correct?

Q4: TFPHTTPServer has property Address. I reckon it will be set to 'localhost' here. Is it correct?

Q5: TFPHTTPServer has no property Username and Password. How do I check the values I set in the corresponding properties of TFPHTTPClient?

Q6: what is the use of parameter Handled?

I apologize if I overlooked the relevant documentation.

Thank you

Peppe

Reply by email

Michael Van Canneyt
to duilio foschi
duilio foschi wrote 62 lines
I would like to write some code where a TFPHttpClient component sends a minimal json payload to a TFPHttpServer that (reads the payload). The working of the TFPHttpClient component is clear enough, while its counterpart TFPHttpServer proves rather obscure to me. TFPHttpClient =========== Page https://wiki.freepascal.org/fphttpclient contains complete and clear code: var Client: TFPHttpClient; Response: TStringStream; Params: string = '{"Name": "John", "Age": 32}'; begin Client := TFPHttpClient.Create(nil); Client.AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)'); Client.AddHeader('Content-Type', 'application/json; charset=UTF-8'); Client.AddHeader('Accept', 'application/json'); Client.AllowRedirect := true; Client.UserName := USER_STRING; Client.Password := PASSW_STRING; Client.RequestBody := TRawByteStringStream.Create(Params); Response := TStringStream.Create(''); try try Client.Post(TheURL, Response); Writeln('Response Code: ' + IntToStr(Client.ResponseStatusCode)); // better be 200 except on E: Exception do Writeln('Something bad happened: ' + E.Message); end; finally Client.RequestBody.Free; Client.Free; Response.Free; end; I reckon I need to set - say- TheURL:='http://localhost:9200'; here TFPHTTPServer ============= say we use TTestHTTPServer=class(TFPHTTPServer); for our server, I understand that all action takes place inside procedure DataModuleRequest (so far so good): procedure TTestHTTPServer.DataModuleRequest(Sender: TObject; ARequest: TRequest; AResponse: TResponse; var Handled: Boolean); begin // end; Q1: how can I create an object of type jsondata from ARequest?

include fpjson and jsonparser units in your uses clause. Then do

JSON :=GetJSON(aRequest.Content);
Q2: can I get a UTF8 string from ARequest?

Yes, aRequest.Content is a string.

Q3: TFPHTTPServer has property Port. I reckon it will be set to 9200 here. Is it correct?

Yes.

Q4: TFPHTTPServer has property Address. I reckon it will be set to 'localhost' here. Is it correct?

It depends on what you want. Best is to leave it empty.

Q5: TFPHTTPServer has no property Username and Password. How do I check the values I set in the corresponding properties of TFPHTTPClient?

aRequest.Authorization will be a string of the form

Basic NNNNNNN

Where NNNNN is a base64 encoded string with username:password in it. So you must decode the string (see bas64 unit) and split the resulting string in username and password.

Q6: what is the use of parameter Handled?

If Handled is false, then an error will be sent to the client. You should set Handled to true.

Michael.

Reply by email

Mattias Gaertner
to Michael Van Canneyt

On Tue, 3 Jan 2023 23:58:42 +0100 (CET)
Michael Van Canneyt via fpc-pascal

Show quoted text 5 lines
[...] > Q4: TFPHTTPServer has property Address. I reckon it will be set to > 'localhost' here. Is it correct? It depends on what you want. Best is to leave it empty.

That will use 0.0.0.0, which is every interface. For security reasons the default should be more limited, like 127.0.0.1, should it not?

Mattias

Reply by email

duilio foschi
to Michael Van Canneyt: include fpjson and jsonparser units in your uses clause. Then do JSON…

Now everything is clear.

Thank you

Peppe

Il Mar 3 Gen 2023 22:59 Michael Van Canneyt via fpc-pascal <

Show quoted text 117 lines
ha scritto: On Tue, 3 Jan 2023, duilio foschi via fpc-pascal wrote: > I would like to write some code where a TFPHttpClient component sends > a minimal json payload to a TFPHttpServer that (reads the payload). > > The working of the TFPHttpClient component is clear enough, while its > counterpart TFPHttpServer proves rather obscure to me. > > TFPHttpClient > =========== > Page https://wiki.freepascal.org/fphttpclient > contains complete and clear code: > > > var > Client: TFPHttpClient; > Response: TStringStream; > Params: string = '{"Name": "John", "Age": 32}'; > begin > Client := TFPHttpClient.Create(nil); > Client.AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)'); > Client.AddHeader('Content-Type', 'application/json; charset=UTF-8'); > Client.AddHeader('Accept', 'application/json'); > Client.AllowRedirect := true; > Client.UserName := USER_STRING; > Client.Password := PASSW_STRING; > Client.RequestBody := TRawByteStringStream.Create(Params); > Response := TStringStream.Create(''); > try > try > Client.Post(TheURL, Response); > Writeln('Response Code: ' + > IntToStr(Client.ResponseStatusCode)); // better be 200 > except on E: Exception do > Writeln('Something bad happened: ' + E.Message); > end; > finally > Client.RequestBody.Free; > Client.Free; > Response.Free; > end; > > I reckon I need to set - say- > TheURL:='http://localhost:9200'; > here > > > TFPHTTPServer > ============= > > say we use > TTestHTTPServer=class(TFPHTTPServer); > for our server, > > I understand that all action takes place inside procedure > DataModuleRequest (so far so good): > > procedure TTestHTTPServer.DataModuleRequest(Sender: TObject; ARequest: TRequest; > AResponse: TResponse; var Handled: Boolean); > begin > // > end; > > Q1: how can I create an object of type jsondata from ARequest? include fpjson and jsonparser units in your uses clause. Then do JSON :=GetJSON(aRequest.Content); > > Q2: can I get a UTF8 string from ARequest? Yes, aRequest.Content is a string. > > Q3: TFPHTTPServer has property Port. I reckon it will be set to 9200 > here. Is it correct? Yes. > > Q4: TFPHTTPServer has property Address. I reckon it will be set to > 'localhost' here. Is it correct? It depends on what you want. Best is to leave it empty. > > Q5: TFPHTTPServer has no property Username and Password. How do I > check the values I set in the corresponding properties of > TFPHTTPClient? aRequest.Authorization will be a string of the form Basic NNNNNNN Where NNNNN is a base64 encoded string with username:password in it. So you must decode the string (see bas64 unit) and split the resulting string in username and password. > > Q6: what is the use of parameter Handled? If Handled is false, then an error will be sent to the client. You should set Handled to true. Michael. _______________________________________________ fpc-pascal maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

Michael Van Canneyt
to Mattias Gaertner: On Tue, 3 Jan 2023 23:58:42 +0100 (CET) Michael Van Canneyt via fpc-pascal That will use…
Mattias Gaertner wrote 13 lines
On Tue, 3 Jan 2023 23:58:42 +0100 (CET) Michael Van Canneyt via fpc-pascal wrote: > [...] >> Q4: TFPHTTPServer has property Address. I reckon it will be set to >> 'localhost' here. Is it correct? > > It depends on what you want. Best is to leave it empty. That will use 0.0.0.0, which is every interface. For security reasons the default should be more limited, like 127.0.0.1, should it not?

Depends on what you want to use it for. For production, I would be surprised if it had to be 127.0.0.1...

Michael.

Reply by email