If you want to limit the time for a cURL Request (HTTP/HTTPS/FTP/SFTP and other protocols) to try to connect to a provided host you can make use of the --connect-timeout option followed by the time specified in seconds.
--connect-timeout <fractional seconds>
--connect-timeout provided will be the maximum time in seconds that the curl connection can take to get back. If the connection is established the cURL request will continue, if not then it will exit.
Note: Since cURL version 7.32.0 connection timeout option accepts time in decimal values as well.
Let's see few examples with --connect-timeout options,
Example 1: cURL HTTP GET with Connection Timeout of 30 seconds
% curl --connect-timeout 30 http://10.20.30.1 curl: (28) Connection timeout after 30001 ms
As you can see the above HTTP GET cURL request failed to connect to the host IP address within 30 seconds so cURL exited with a timeout exit code (28)
Example 2: cURL HTTPS POST with Connection Timeout of 60 seconds
% curl --connect-timeout 60 --request POST '10.20.1.1' \ --header 'Content-Type: text/html' \ --header 'Accept-Encoding: deflate, gzip;q=1.0, *;q=0.5' curl: (28) Connection timeout after 60006 ms
Example 3: cURL HTTPS GET with Connection Timeout set in fractions (decimals)
% curl --connect-timeout 5.5 https://10.20.30.1 curl: (28) Connection timeout after 5505 ms
The --max-time option is what you can use with cURL in order to exit cURL even if the connection was established but the request was not completed.
You can also make use of the short-hand option parameter -m
Example 4: max timeout with 0.3 seconds
% curl --max-time 0.3 https://code2care.org curl: (28) Operation timed out after 300 milliseconds with 14501 out of 80674 bytes received
As you may see in the above example, the max timeout is set to 300 milliseconds, in this duration, the connection to the host was established and 14501 bytes of the response data was received, but as the max time was elapsed, the curl exited even though the data transfer was in progress.
Have Questions? Post them here!
- cURL Example using IP (IPv4 and IPv6) address and Port
- Efficient way to perform HTTP cURL GET/POST Requests with Payload
- Save cURL Command Output to a external file
- How to set Connection and Max Timeout for cURL Request with Examples
- [fix] command not found curl
- Fetch only content-type using cURL Command
- Trace using cURL Command Example
- Hide cURL Outputs and Errors on Terminal
- cURL Option to show HTTP Response Status Code
- Downloading Google Chrome using cURL Command
- Perform Basic Authentication using cURL with Examples
- List of 28 Protocols supported by cURL with Examples
- URL Encode cURL HTTP GET\POST Data with Examples
- Get the Size of HTTP Response using cURL Command (Content Length)
- How to get cURL Command to run in verbose mode?
- Get cURL command from Chrome Inspect Network HTTP URL
- Encode or Decode Base64 String using Mac Terminal Command - MacOS
- [Fix] Microsoft 53003 Error - Microsoft
- SwissCovid App Launched in Switzerland - News
- [fix] URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs) IntelliJ - Java
- Hide Bluetooth icon on Mac Menu Bar macOS Ventura 13 - MacOS
- Remove Html head and body tags from ckeditor source - Html
- GitHub: How to Search Code - Git
- java: unclosed string literal [Error] - Java