LibCurl: If the API doesn't return any data, the app freezes

Hello everyone!
I’m having the following issue:
I’m sending data to an API.
If the API doesn’t return any data, the app freezes.
The sending line is:

res = curl.SendRequest(Send:Url, Send:Total, respBuffer)

IF res = CURLE_OK
 sz#=respBuffer.StrLen()
 stReturn &=New STRING(sz#+1)
 stReturn = respBuffer.Str()
 ?txtHttpResponse{PROP:Text}= clip(stReturn)
ELSEIF res = -1
 ?txtHttpResponse{PROP:Text}=('SendRequest ROUTINE: Cannot open local file -> libcurl')
ELSE
 ?txtHttpResponse{PROP:Text}=('SendRequest ROUTINE: SendRequest failed: '& curl.StrError(res))
END

I looked in the cUrl class for a timeout or max_time, but I couldn’t find it.

I put a message after the

res = curl.SendRequest(Send:Url, Send:Total, respBuffer)
Message('hie')

But it freezes before the message is displayed if the API doesn’t respond…

Does anyone know how to solve this?

Thanks…!

Never used the product, but was easily able to find the how-to on the repo, and search for timeout.

This is the cod to send

SendRequest         routine
DATA
curl        TCurlHttpClass
res CURLcode
qIndex      LONG, AUTO
respBuffer  &IDynStr 

CODE
    
    respBuffer &= NewDynStr()
    curl.Init()
    curl.FreeHttpHeaders() 
    IF RECORDS(HttpHeaders)
        LOOP qIndex = 1 TO RECORDS(HttpHeaders)
            GET(HttpHeaders, qIndex)
            curl.AddHttpHeader(HttpHeaders.HttpHeader)
        END
        res = curl.SetHttpHeaders()
    END
    
    res = curl.SetCustomRequest(Send:CustomRequest)
    res = curl.SendRequest(Send:Url, Send:Total, respBuffer)

    !*I think here a -max-time or -timeout or -m*
    
    IF res = CURLE_OK
        sz#=respBuffer.StrLen()
        stReturn &=New STRING(sz#+1)
        stReturn = respBuffer.Str()
        ?txtHttpResponse{PROP:Text}= clip(stReturn)
    ELSIF res = -1
        ?txtHttpResponse{PROP:Text}=('SendRequest ROUTINE: Cannot open local file -> libcurl')
    ELSE
        ?txtHttpResponse{PROP:Text}=('SendRequest ROUTINE: SendRequest failed: '& curl.StrError(res))
    END
    
    DISPOSE(stReturn)
    DisposeDynStr(respBuffer)
    
    DISPLAY()

I can’t find the max_timeout in the code.

Hi

I haven’t used the class for a little while but had my AI analyse Mikes code, it says do the following.

For SendRequest timeouts, use SetOpt before calling SendRequest:

 ! Timeout in seconds
 curl.SetOpt(CURLOPT_TIMEOUT, 30)
 
 ! Or in milliseconds (more precise)
 curl.SetOpt(CURLOPT_TIMEOUT_MS, 30000)
 
 curl.SendRequest(url, postFields, responseFile)

If the operation times out, SendRequest returns CURLE_OPERATION_TIMEDOUT.

I hope that helps.

Mark

Hi Mark, I will try and I return here
thanks!!!

Okay, apparently curl.SetOpt(CURLOPT_TIMEOUT_MS, 30000) stops waiting for the server response at the desired time.

The problem is that it’s the total service time:
Connection,
File loading,
Reading the response.
It’s not just for the response itself.
So I’m testing with this 30-second time.

There are other operators:
curl.SetOpt(CURLOPT_CONNECTTIMEOUT, 10)
and
curl.SetOpt(CURLOPT_SERVER_RESPONSE_TIMEOUT, 10)

I tried using them, dropping the API to see if they worked, to see if the application wouldn’t freeze waiting for the response.
But it didn’t work.

Have you tried using a standalone REST client, such as Postman, or Insomnia to check what happens when you submit your request to the server? In my experience using these tools removes a lot of the guesswork as to what should work and what doesn’t, without worrying about what might be wrong in Clarion.

Hi, thanks for the reply, but the problem is when the server or API doesn’t return data.
The curl loop freezes the system indefinitely until something returns, and sometimes we have to restart the program to stop the curl.

I’m already researching what the alternatives are.

The curl.SetOpt(CURLOPT_SERVER_RESPONSE_TIMEOUT, Long) command doesn’t seem to work.

I tested it, but the app still crashed.

Just curious if this is part of the problem, but whats your default?

  • Path: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters
  • Value Name: KeepAliveTime (DWORD)
  • Value Data: Time in milliseconds.

By default, Windows sets this to 2 hours ( 7,200,000 milliseconds).

Recommended Setting: In many environments (e.g., server applications), it is recommended to reduce this to 5 minutes ( 300,000 ms) or lower to identify dead connections faster.

Obviously you could reduce your test machine to maybe 30 seconds (30,000 ms) to see if this setting is the root cause and if it is, its bought you some time until a better solution appears.

Sorry to harp on it Jorge but, have you tried using a standalone REST client, such as Postman, or Insomnia to check what happens when you submit your request to the server?

You say you get no response, if I was doing this I’d really like to know what these tools do in this situation?

You also say that the curl loop freezes the system and separately, that it crashes - are these these the same thing?

Hello..!
Yes, we’ve already checked, it doesn’t return anything, it remains unanswered, either momentarily or because the API or server crashed.
Then the waiting system freezes, that’s it.