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