Page 1 of 1
HTTPRequest Timeout
Posted: Fri Jan 22, 2021 10:53 pm
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
Re: HTTPRequest Timeout
Posted: Sat Jan 23, 2021 6:06 am
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
Re: HTTPRequest Timeout
Posted: Thu Sep 07, 2023 3:33 pm
by skywalk
Thanks TI-994A
Having network problems and this helps speed up detection.
Re: HTTPRequest Timeout
Posted: Thu Sep 07, 2023 8:55 pm
by Caronte3D
Also, you can use libcurl throug the excelent libcurl.pbi by infratec
Re: HTTPRequest Timeout
Posted: Thu Sep 07, 2023 9:22 pm
by infratec
Or simply use:
Code: Select all
HTTPTimeout(ConnectTimeout [, GlobalTimeout])
Available since PureBasic 6.02 beta 3
Re: HTTPRequest Timeout
Posted: Thu Sep 07, 2023 9:32 pm
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

I missed it

Re: HTTPRequest Timeout
Posted: Thu Sep 07, 2023 9:45 pm
by skywalk
Haha, I missed it too.
I looked briefly, thinking it was in the request parameters.
Thanks!
Re: HTTPRequest Timeout
Posted: Fri Sep 08, 2023 6:40 am
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().