HTTPRequest Timeout

Everything else that doesn't fall into one of the other PB categories.
swhite
Enthusiast
Enthusiast
Posts: 727
Joined: Thu May 21, 2009 6:56 pm

HTTPRequest Timeout

Post by swhite »

Hi

If I use the HTTPRequest for a synchronous request how long will the request wait for a response? I am accessing a REST API that waits for user input and I am concerned that if the user never responds that the request will wait indefinitely and I would like to ensure that wait is no more than 30 seconds.

Thanks,
Simon
Simon White
dCipher Computing
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: HTTPRequest Timeout

Post by TI-994A »

swhite wrote:...HTTPRequest for a synchronous request how long will the request wait for a response? I would like to ensure that wait is no more than 30 seconds.
Technically, HTTPRequests could wait indefinitely. Timeouts must be manually set, either on the client or server side. For synchronous requests, simply initiate the connection within a thread, and implement a timer to kill the thread at the required timeout. Here's an example; to test the timeout results, substitute the server accordingly.

Code: Select all

InitNetwork()

Procedure httpRequestProc(*arg)
 
  ;substitute with an unresponsive server to test the timeout results
  HttpRequest = HTTPRequest(#PB_HTTP_Get, "https://www.google.com", "")
  
  If HttpRequest
    SetGadgetText(1, "HTTP Request completed.")
    FinishHTTP(HTTPRequest)
  Else
    SetGadgetText(1, "HTTP Request creation failed!")
  EndIf
  
EndProcedure

wFlags = #PB_Window_SystemMenu | #PB_Window_ScreenCentered
OpenWindow(0, #PB_Ignore, #PB_Ignore, 400, 200, "HTTP Request", wFlags)
TextGadget(0, 20, 20, 360, 40, "Time: 00:00:00", #PB_Text_Right)
TextGadget(1, 20, 80, 360, 40, "HTTP Request...", #PB_Text_Center)
AddWindowTimer(0, 0, 1000)

HTTPRequestTimeout = 30 * 1000  ;30 seconds
httpRequestThreadStart = ElapsedMilliseconds()
httpRequestThread = CreateThread(@httpRequestProc(), 0)

Repeat
  Select WaitWindowEvent()
      
    Case #PB_Event_CloseWindow
      appQuit = #True
      
    Case #PB_Event_Timer      
      SetGadgetText(0, "Time: " + FormatDate("%hh:%ii:%ss", Date()))      
      
      If IsThread(httpRequestThread) And 
         ElapsedMilliseconds() - httpRequestThreadStart > HTTPRequestTimeout
        KillThread(httpRequestThread)
        SetGadgetText(1, "HTTP Request timed out! Thread killed.")
      EndIf
      
  EndSelect
Until appQuit
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
skywalk
Addict
Addict
Posts: 4000
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: HTTPRequest Timeout

Post by skywalk »

Thanks TI-994A :)
Having network problems and this helps speed up detection.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Caronte3D
Addict
Addict
Posts: 1055
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: HTTPRequest Timeout

Post by Caronte3D »

Also, you can use libcurl throug the excelent libcurl.pbi by infratec
Last edited by Caronte3D on Thu Sep 07, 2023 9:28 pm, edited 1 time in total.
infratec
Always Here
Always Here
Posts: 6874
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: HTTPRequest Timeout

Post by infratec »

Or simply use:

Code: Select all

HTTPTimeout(ConnectTimeout [, GlobalTimeout])
Available since PureBasic 6.02 beta 3
User avatar
Caronte3D
Addict
Addict
Posts: 1055
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: HTTPRequest Timeout

Post by Caronte3D »

infratec wrote: Thu Sep 07, 2023 9:22 pm Or simply use:

Code: Select all

HTTPTimeout(ConnectTimeout [, GlobalTimeout])
Available since PureBasic 6.02 beta 3
:shock: :shock: I missed it :? :lol:
User avatar
skywalk
Addict
Addict
Posts: 4000
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: HTTPRequest Timeout

Post by skywalk »

Haha, I missed it too.
I looked briefly, thinking it was in the request parameters.
Thanks!
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
infratec
Always Here
Always Here
Posts: 6874
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: HTTPRequest Timeout

Post by infratec »

skywalk wrote: Thu Sep 07, 2023 9:45 pm I looked briefly, thinking it was in the request parameters.
I did this in my HTTPRequestI().
Post Reply