TFPHttpClient timeout error lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- jhorta · 2024-11-25 20:40 Im trying to connect and post to an api running on my local machine using TFPHttpClient but im getting a timeout error. Everything Works fine when i call via Postman. I tried to use the ConnectTimeout property(60) but I kept getting the same error. Any tips? Could I be missing something? Running on Windows 10, Lazarus 3.4 Error : Connection to localhost:4000 timed out. My code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Procedure Send;Var Client: TFPHttpClient;    Response: TStringStream;Begin   Client := TFPHttpClient.Create(nil);   Client.AddHeader('Content- Type', 'application/json; charset=UTF-8');   Client.RequestBody := TRawByteStringStream.Create('{"jwt": "", ' + '"codAplicacao": "F60EB930-9B1B-11EF-9657-02240D86274B"}');    Response := TStringStream.Create('');    Try      Try         Client.Post('http://localhost:4000/local/inicio/hortech', Response);         GeraLog('Response: ' + String(Response));       Except on E: Exception do          Begin             GeraLog('Something wrong: ' + E.Message);          End;       End;   Finally       Client.RequestBody.Free;       Client.Free;       Response.Free;   End;End; ---------------------------------------------------------------------- jhorta · 2024-11-28 01:49 I tried a few things but nothing worked. A bit frustrating. I should add that during some testing, I noticed that the error message occurs for any url, even if it doesn't exist or is unavailable. I appreciate any tips, links, etc. ---------------------------------------------------------------------- paweld · 2024-11-28 05:55 The error may be due to the fact that the program is blocked on the firewall. Overall the code is almost OK, only you have an error when get the response string - it should be:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i GeraLog('Response: ' + Response.DataString);instead of: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i GeraLog('Response: ' + String(Response)); ---------------------------------------------------------------------- Thaddy · 2024-11-28 06:36 ConnectTimeout property, I believe 60 are ms, so make it much higher, like 1000. A timeout of 60ms is useless. ---------------------------------------------------------------------- jhorta · 2024-11-28 18:42 ConnectTimeout set to 1000, 2000, 3000... permission for the program configured in the firewall Firewall disabled Nothing worked ---------------------------------------------------------------------- TRon · 2024-11-28 22:27 @jhorta: must be your setup somewhere because when testing it times-out almost immediate for me. There are sites to test these kind of things such as JSONPlaceholder and reqbin and I am sure there are many others. So, your postman also post its requests insecure ? ---------------------------------------------------------------------- jhorta · 2024-11-29 13:56 must be your setup somewhere because when testing it times-out almost immediate for me. Maybe. Does anyone know of any way to trace the api call to analyze its contents? So, your postman also post its requests insecure ? The firewall ask about and i give permission. After that i can see the entry in firewall panel. I added the same permission to my program(Project1.exe). It didn't work ---------------------------------------------------------------------- Thaddy · 2024-11-29 16:44 The full program or a reproducable example...Because in the case of everyone else it works.. This is really frustrating. I am an old man with time to test..for free.. ---------------------------------------------------------------------- jhorta · 2024-11-29 19:07 Here is the complete code, in case you ever have that "philanthropic" moment. The interface consists of an edit button to show the result and two buttons, one for local access and another to access the same application in the cloud (this one has an SSL problem, which is in another topic on this forum). The application is an AWS lambda, developed in Python, running locally using the Serverless Framework. I even thought that this could be the problem, but it works normally via Postman. As you said, it is really frustrating. To avoid being stuck indefinitely, since I already have the entire backend built, I decided to download Delphi Community. It took me a while to find the correct syntax for TRESTClient, but everything worked well, locally and in the cloud. That gives me a year to try to solve it in FPC. Cheers. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, fphttpclient, fpjson, opensslsockets; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Button2: TButton;    Edit1: TEdit;    procedure Button1Click(Sender: TObject);    procedure Button2Click(Sender: TObject);  private   public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } Function Send(Url : String) : String;Var Client: TFPHttpClient;    Response: TStringStream;Begin   Client := TFPHttpClient.Create(nil);   Client.ConnectTimeout := 6000;   Client.AddHeader('Content-Type', 'application/json');   Client.AddHeader('Accept', 'application/json');   Client.RequestBody := TRawByteStringStream.Create('{"jwt": "", "codAplicacao": "F60EB930-9B1B-11EF-9657-02240D86274B"}');    Response := TStringStream.Create('');    Try      Try         Client.Post(Url, Response);         Result := 'Response: ' + Response.DataString;       Except on E: Exception do          Begin             Result := 'Something wrong: ' + E.Message;          End;       End;   Finally     Client.RequestBody.Free;       Client.Free;       Response.Free;   End;End; procedure TForm1.Button1Click(Sender: TObject);begin   Edit1.Text := Send('https://mystack.execute-api.us- east-2.amazonaws.com/dev/inicio/hortech');end; procedure TForm1.Button2Click(Sender: TObject);begin   Edit1.Text := Send('http://localhost:4000/local/inicio/hortech');end; end. ---------------------------------------------------------------------- Thaddy · 2024-11-29 21:01 If you do not run a server on localhost, of course you can not test your code..... That is after testing my conclusion: there is no server. ---------------------------------------------------------------------- Leledumbo · 2024-12-07 15:56 Quote from: jhorta on November 25, 2024, 09:40:16 pmAny tips? Could I be missing something? Without knowing exactly what your server side expects, it's a guessing game. I don't like guessing game. But, your client code is fine. Here I extract it to a simple compilable runnable program: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode objfpc}{$H+} uses Classes,SysUtils,fphttpclient, fpjson, opensslsockets; Function Send(Url : String) : String;Var Client: TFPHttpClient;    Response: TStringStream;Begin   Client := TFPHttpClient.Create(nil);   Client.ConnectTimeout := 6000;   Client.AddHeader('Content-Type', 'application/json');   Client.AddHeader('Accept', 'application/json');   Client.RequestBody := TRawByteStringStream.Create('{"jwt": "", "codAplicacao": "F60EB930-9B1B-11EF-9657-02240D86274B"}');    Response := TStringStream.Create('');    Try      Try         Client.Post(Url, Response);         Result := 'Response: ' + Response.DataString;       Except on E: Exception do          Begin             Result := 'Something wrong: ' + E.Message;          End;       End;   Finally     Client.RequestBody.Free;       Client.Free;       Response.Free;   End;End; begin  WriteLn(Send('http://localhost:4000/local/inicio/hortech'));end. with the corresponding server: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode objfpc}{$H+} uses    httpdefs,    httproute,    fpjson,    jsonparser,    fphttpapp; procedure whatever(AReq: TRequest; AResp: TResponse);var  LJSONReqBody: TJSONObject;  LCodAplicacao: TJSONData;begin  LJSONReqBody := TJSONObject(GetJSON(AReq.Content));  LCodAplicacao := LJSONReqBody.FindPath('codAplicacao');  if Assigned(LCodAplicacao) then    AResp.Content := LCodAplicacao.AsString  else    AResp.Content := 'codAplicacao not supplied';end; begin   HTTPRouter.RegisterRoute('*', @whatever);   Application.Initialize;   Application.Port := 4000;   Application.Run;end. This is the best that I can do given everything you've informed. You need to check whether the Python server side expects certain headers. Postman does act like a real browser so it sends the usual browser headers, not only content-type and accept as you tell fphttpclient to. Some servers are typically picky when some of those headers aren't present (e.g.: user-agent, accept-encoding, connection, etc.). ---------------------------------------------------------------------- geraldholdsworth · 2025-01-23 12:11 Can I jump onto this post with a similar issue? I'm getting Connect to www.bing.com:443 failed (I'm actually trying to write some code to talk to a Sennheiser MobileConnect, but if I can't connect to Bing, what hope have I got of connecting to this device?) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses Classes, SysUtils, Forms, Controls, Graphics, Dialogs,ExtCtrls,StdCtrls, fphttpclient, opensslsockets, openssl; type  { TForm1 }  TForm1 = class(TForm)  Button1:TButton;  Memo1:TMemo;  Panel1:TPanel;  procedure Button1Click(Sender:TObject); private  const token ='NjkxNThkZTAtYjIwNC00NzQ4LWJlYjMtYWIwN2UyMjViZTIw';  const sernum='1423003482';  const IPaddr='192.168.0.180:443'; public  end; var Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender:TObject);var  Client: TFPHttpClient;  Response: TStringStream;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.AddHeader('Authorization','Basic '+token);  Client.AllowRedirect := true;  Response := TStringStream.Create('');  try    try      //Client.Post('https://'+IPaddr+'/public/api/v3.0/stat ions/'+sernum+'/channels/1/access/qrcode.data', Response);      Memo1.Lines.Add(Client.Get('https://www.bing.com'));//      Memo1.Lines.Add('Response Code: ' + IntToStr(Client.ResponseStatusCode)); // better be 200    except on E: Exception do      Memo1.Lines.Add(E.Message);    end;  finally    Client.Free;    Response.Free;  end;end; end. Got this from here. ---------------------------------------------------------------------- two_oceans · 2025-07-15 02:48 Hello. I'd like suggest possible reason of problem in first message of topic. Applicable if program can't access to target port in range ~4000-7000 on Windows server. Some mechanics keep this ports locked. It's not Windows firewall, something else. For me it was Windows Server 2016 and port 6033 (like mirror of standart mysql port 3306). No program could access mysql service on this port (localhost or 127.0.0.1 or 192.168.x.x address), but it perfectly worked when I changed port of service and port in program to 633. So when I see topic starter mention port 4000 I remember this case immediately. I believe previous message about bing and port 433 is not simular issue and misplaced in this topic. ---------------------------------------------------------------------- Thaddy · 2025-07-15 03:52 TfpHttpclient and the way openssl is handled have changed considerably since fpc 2.7.1 The example in the wiki can be reduced to:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode delphi}{$ifdef windows}{$apptype console}{$endif} uses  fphttpclient,  openssl,   opensslsockets; var  Client: TFPHttpClient; begin  Client := TFPHttpClient.Create(nil);  try    Client.AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)');    { Allow redirections }    Client.AllowRedirect := true;    writeln(Client.Get('https://google.com/'));   finally    Client.Free;  end;end.Tested. OpenSSL initialization is automatic in 3.2.0 or higher. FPC 3.3.1 can use openssl 3. Ergo: the wiki needs an overhaul, it is not current. Lazarus version, add lcl to your project dependencies.:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program clienttest;{$mode delphi}{$ifdef windows}{$apptype console}{$endif} uses  {$IFDEF UNIX}  cthreads,  {$ENDIF}  Interfaces,lclintf, sysutils,classes,fphttpclient,openssl,opensslsockets; var  Client: TFPHttpClient;  List:TStringList;begin  Client := TFPHttpClient.Create(nil);  List := TStringlist.Create;  try    Client.AddHeader('User-Agent', 'Mozilla/5.0 (compatible; fpweb)');    { Allow redirections }    Client.AllowRedirect := true;    List.Text:=Client.Get('https://google.com/');    List.SaveToFile('c:\users\public\atest.html'); // change paths for Linux. './atest.html';    OpenURL('C:\users\public\atest.html');  finally    List.Free;    Client.Free;  end;end. If there are still problems with the connection to your server, let me know: I do not have such a device, so I basically need to know what protocol it uses and what port. At least now you have client code that is known to work. I have many Raspberry's that run server code that can connect in the same way as above. Note this code requires a recent OpenSSL and I used OpenSSL 3. If it works against google, you can be assured there is no communications problem at the client side. Plz use a port above 1000, though as per IANA documentation. Port 633 is reserved for service status updates. Your original 4000 should be OK.