Recent

Author Topic: TFPHttpClient timeout error  (Read 1868 times)

jhorta

  • New member
  • *
  • Posts: 7
TFPHttpClient timeout error
« on: November 25, 2024, 09:40:16 pm »
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][+][-]
  1. Procedure Send;
  2. Var Client: TFPHttpClient;
  3.     Response: TStringStream;
  4. Begin
  5.    Client := TFPHttpClient.Create(nil);
  6.    Client.AddHeader('Content-Type', 'application/json; charset=UTF-8');
  7.    Client.RequestBody := TRawByteStringStream.Create('{"jwt": "", ' + '"codAplicacao": "F60EB930-9B1B-11EF-9657-02240D86274B"}');
  8.  
  9.    Response := TStringStream.Create('');
  10.  
  11.    Try
  12.       Try
  13.          Client.Post('http://localhost:4000/local/inicio/hortech', Response);
  14.          GeraLog('Response: ' + String(Response));
  15.        Except on E: Exception do
  16.           Begin
  17.              GeraLog('Something wrong: ' + E.Message);
  18.           End;
  19.        End;
  20.    Finally
  21.        Client.RequestBody.Free;
  22.        Client.Free;
  23.        Response.Free;
  24.    End;
  25. End;
  26.  

jhorta

  • New member
  • *
  • Posts: 7
Re: TFPHttpClient timeout error
« Reply #1 on: November 28, 2024, 02:49:45 am »
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

  • Hero Member
  • *****
  • Posts: 1268
Re: TFPHttpClient timeout error
« Reply #2 on: November 28, 2024, 06:55:17 am »
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][+][-]
  1. GeraLog('Response: ' + Response.DataString);
instead of:
Code: Pascal  [Select][+][-]
  1. GeraLog('Response: ' + String(Response));
Best regards / Pozdrawiam
paweld

Thaddy

  • Hero Member
  • *****
  • Posts: 16187
  • Censorship about opinions does not belong here.
Re: TFPHttpClient timeout error
« Reply #3 on: November 28, 2024, 07:36:57 am »
ConnectTimeout property, I believe 60 are ms, so make it much higher, like 1000. A timeout of 60ms is useless.
« Last Edit: November 28, 2024, 07:39:02 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

jhorta

  • New member
  • *
  • Posts: 7
Re: TFPHttpClient timeout error
« Reply #4 on: November 28, 2024, 07:42:33 pm »
ConnectTimeout set to 1000, 2000, 3000...

permission for the program configured in the firewall

Firewall disabled

Nothing worked

TRon

  • Hero Member
  • *****
  • Posts: 3641
Re: TFPHttpClient timeout error
« Reply #5 on: November 28, 2024, 11:27:41 pm »
@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 ?
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

jhorta

  • New member
  • *
  • Posts: 7
Re: TFPHttpClient timeout error
« Reply #6 on: November 29, 2024, 02:56:44 pm »
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

  • Hero Member
  • *****
  • Posts: 16187
  • Censorship about opinions does not belong here.
Re: TFPHttpClient timeout error
« Reply #7 on: November 29, 2024, 05:44:09 pm »
The full program or a reproducable example...Because in the case of everyone else it works.. >:D
This is really frustrating. I am an old man with time to test..for free..
« Last Edit: November 29, 2024, 05:46:42 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

jhorta

  • New member
  • *
  • Posts: 7
Re: TFPHttpClient timeout error
« Reply #8 on: November 29, 2024, 08:07:47 pm »
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][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, fphttpclient, fpjson, opensslsockets;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Button2: TButton;
  17.     Edit1: TEdit;
  18.     procedure Button1Click(Sender: TObject);
  19.     procedure Button2Click(Sender: TObject);
  20.   private
  21.  
  22.   public
  23.  
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. { TForm1 }
  34.  
  35. Function Send(Url : String) : String;
  36. Var Client: TFPHttpClient;
  37.     Response: TStringStream;
  38. Begin
  39.    Client := TFPHttpClient.Create(nil);
  40.    Client.ConnectTimeout := 6000;
  41.    Client.AddHeader('Content-Type', 'application/json');
  42.    Client.AddHeader('Accept', 'application/json');
  43.    Client.RequestBody := TRawByteStringStream.Create('{"jwt": "", "codAplicacao": "F60EB930-9B1B-11EF-9657-02240D86274B"}');
  44.  
  45.    Response := TStringStream.Create('');
  46.  
  47.    Try
  48.       Try
  49.          Client.Post(Url, Response);
  50.          Result := 'Response: ' + Response.DataString;
  51.        Except on E: Exception do
  52.           Begin
  53.              Result := 'Something wrong: ' + E.Message;
  54.           End;
  55.        End;
  56.    Finally
  57.        Client.RequestBody.Free;
  58.        Client.Free;
  59.        Response.Free;
  60.    End;
  61. End;
  62.  
  63. procedure TForm1.Button1Click(Sender: TObject);
  64. begin
  65.    Edit1.Text := Send('https://mystack.execute-api.us-east-2.amazonaws.com/dev/inicio/hortech');
  66. end;
  67.  
  68. procedure TForm1.Button2Click(Sender: TObject);
  69. begin
  70.    Edit1.Text := Send('http://localhost:4000/local/inicio/hortech');
  71. end;
  72.  
  73. end.
  74.  

Thaddy

  • Hero Member
  • *****
  • Posts: 16187
  • Censorship about opinions does not belong here.
Re: TFPHttpClient timeout error
« Reply #9 on: November 29, 2024, 10:01:19 pm »
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.
If I smell bad code it usually is bad code and that includes my own code.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8776
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: TFPHttpClient timeout error
« Reply #10 on: December 07, 2024, 04:56:00 pm »
Any 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][+][-]
  1. {$mode objfpc}{$H+}
  2.  
  3. uses Classes,SysUtils,fphttpclient, fpjson, opensslsockets;
  4.  
  5. Function Send(Url : String) : String;
  6. Var Client: TFPHttpClient;
  7.     Response: TStringStream;
  8. Begin
  9.    Client := TFPHttpClient.Create(nil);
  10.    Client.ConnectTimeout := 6000;
  11.    Client.AddHeader('Content-Type', 'application/json');
  12.    Client.AddHeader('Accept', 'application/json');
  13.    Client.RequestBody := TRawByteStringStream.Create('{"jwt": "", "codAplicacao": "F60EB930-9B1B-11EF-9657-02240D86274B"}');
  14.  
  15.    Response := TStringStream.Create('');
  16.  
  17.    Try
  18.       Try
  19.          Client.Post(Url, Response);
  20.          Result := 'Response: ' + Response.DataString;
  21.        Except on E: Exception do
  22.           Begin
  23.              Result := 'Something wrong: ' + E.Message;
  24.           End;
  25.        End;
  26.    Finally
  27.        Client.RequestBody.Free;
  28.        Client.Free;
  29.        Response.Free;
  30.    End;
  31. End;
  32.  
  33. begin
  34.   WriteLn(Send('http://localhost:4000/local/inicio/hortech'));
  35. end.
  36.  
with the corresponding server:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2.  
  3. uses
  4.     httpdefs,
  5.     httproute,
  6.     fpjson,
  7.     jsonparser,
  8.     fphttpapp;
  9.  
  10. procedure whatever(AReq: TRequest; AResp: TResponse);
  11. var
  12.   LJSONReqBody: TJSONObject;
  13.   LCodAplicacao: TJSONData;
  14. begin
  15.   LJSONReqBody := TJSONObject(GetJSON(AReq.Content));
  16.   LCodAplicacao := LJSONReqBody.FindPath('codAplicacao');
  17.   if Assigned(LCodAplicacao) then
  18.     AResp.Content := LCodAplicacao.AsString
  19.   else
  20.     AResp.Content := 'codAplicacao not supplied';
  21. end;
  22.  
  23. begin
  24.    HTTPRouter.RegisterRoute('*', @whatever);
  25.    Application.Initialize;
  26.    Application.Port := 4000;
  27.    Application.Run;
  28. end.
  29.  
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.).

 

TinyPortal © 2005-2018