cURL is a command line tool for transferring data using various network protocols. When making HTTP cURL requests, properly handling timeouts is crucial for a reliable connection. Using cURL, you can set timeout parameters for different stages of the communication process.
Timeouts prevent cURL from hanging indefinitely when a connection or operation takes too long. Appropriate timeouts handle slow connections, unresponsive servers, and large file transfers.
Timeout behavior may vary depending on the specific cURL version and the operating system you’re using.
This guide covers the main timeout command options and provides examples using the cURL command line tool.
These options help prevent your requests from hanging indefinitely and are especially useful in scripts or automated processes, such as web scraping.
Total operation timeout – the maximum time (in seconds) allowed for the whole operation, including DNS resolution, connection time, and data transfer.
Limit total operation time to 30 seconds:
curl --max-time 30 https://oxylabs.io
You can also use the short option:
curl -m 30 https://oxylabs.io
Specify the maximum time allowed for the connection phase only. Allow 5 seconds for initial connection:
curl --connect-timeout 5 https://oxylabs.io
Combine with max-time for more control:
curl --connect-timeout 5 --max-time 30 https://oxylabs.io
Use together to abort transfers (in bytes) that are too slow. Use K, M, and G for KBs, MBs, and GBs.
Abort if the speed drops below 1MB/s for 30 seconds:
curl --speed-limit 1M --speed-time 30 https://oxylabs.io
Combine specified timeout options for a desirable result. This command sets a connect timeout of 10 seconds, a request timeout of 30 seconds, and aborts the transfer if the download speed is below 1MB/s for 10 seconds.
curl -m 10 --max-time 30 --speed-limit 1M --speed-time 10 https://oxylabs.io
Adjust timeouts based on the operation type.
Short timeouts for API endpoints
Longer timeouts for file downloads
Very long timeouts for backup operations
cURL with API request:
curl --connect-timeout 5 \
--max-time 30 \
https://realtime.oxylabs.io
Download files with cURL using generous timeouts and speed monitoring:
curl --connect-timeout 10 \
--max-time 3600 \
--speed-limit 10M \
--speed-time 60 \
--output file.zip \
https://oxylabs.io/file.zip
Retry up to 3 times and wait 2 seconds between retry attempts:
curl --connect-timeout 5 \
--max-time 30 \
--retry 3 \
--retry-delay 2 \
https://oxylabs.io
Configure cURL with proxy request routing using separate timeout controls:
curl --connect-timeout 5 \
--max-time 30 \
--proxy "pr.oxylabs.io:7777" \ # Oxylabs Residential Proxies
--proxy-connect-timeout 3 \
https://oxylabs.io
Always specify both connect and maximum timeouts:
curl --connect-timeout 5 --max-time 30 https://oxylabs.io
Use longer timeouts for large files:
curl --connect-timeout 10 \
--max-time 3600 \
--output file.zip \
https://oxylabs.io/file.zip
Add progress monitoring for long-running transfers:
curl --connect-timeout 10 \
--max-time 3600 \
--progress-bar \
--write-out "\n%{time_total}s total, %{speed_download} bytes/sec average\n" \
https://oxylabs.io/file.zip
Implement multiple layers of timeout protection:
curl --connect-timeout 5 \ # Connection phase
--max-time 30 \ # Overall transfer
--speed-limit 1M \ # Minimum speed
--speed-time 15 \ # Time window for speed check
https://oxylabs.io
Automatically retry failed requests with increasing delays:
curl --connect-timeout 5 \
--max-time 30 \
--retry 3 \
--retry-delay 2 \
--retry-max-time 60 \
--retry-all-errors \
https://oxylabs.io
Add options for comprehensive timeout error handling and logging all transferred data into a file:
curl --connect-timeout 5 \
--max-time 30 \
--verbose \
--trace-ascii curl_trace.log \
--write-out "\nHTTP Code: %{http_code}\nTotal Time: %{time_total}s\n" \
https://oxylabs.io
Add -v for detailed progress and error information:
curl -v --connect-timeout 5 --max-time 30 https://oxylabs.io
Use --head to test connection without downloading:
curl --head --connect-timeout 5 https://oxylabs.io
Always set both connect and maximum timeouts to avoid hanging indefinitely. Otherwise, you might encounter resource leaks (open connections/sockets), poor user experience, and cascading failures in your system.
Use retry logic for resilience.
Adjust timeouts based on operation type: API endpoints, downloads, or database backups.
Include proper monitoring and logging to know precisely what is happening.
Consider network conditions when setting limits: unresponsive servers, rate-limited servers, or download interruptions.
For more information about timeouts and other options, refer to the cURL documentation by running man curl in the terminal or visiting the official cURL website.
In cURL, you can set timeouts using two main options.
--connect-timeout to set the connection timeout:
curl --connect-timeout 5 https://oxylabs.io
--max-time (or -m) to set the maximum time for the operation:
curl --max-time 30 https://oxylabs.io
You can use them together:
curl --connect-timeout 5 --max-time 30 https://oxylabs.io
The values are in seconds. The connect-timeout limits how long cURL will wait to establish a connection, while the max-time limits the entire operation, including the data transfer.
To fix cURL timeout errors, try the following.
Increase the timeout values:
curl --connect-timeout 10 --max-time 60 https://oxylabs.io
If the server is slow or the file is large, set even longer timeouts:
curl --connect-timeout 30 --max-time 300 https://oxylabs.io
Add retry logic:
curl --retry 3 \
--retry-delay 2 \
--connect-timeout 10 \
--max-time 60 \
https://oxylabs.io
If problems persist, check if the server is responsive.
In cURL, there is no built-in maximum limit for execution time. The --max-time (or -m) option can be set to any positive value, including very large numbers. Set a reasonable timeout based on your needs.
Long timeout (1 hour):
curl --max-time 3600 https://oxylabs.io
Very long timeout (24 hours):
curl --max-time 86400 https://oxylabs.io
The cURL response code for timeout is 28 (CURLE_OPERATION_TIMEDOUT). When using cURL in the command line, if a request times out, it’ll exit with status code 28.
curl --max-time 1 https://oxylabs.io
if [ $? -eq 28 ]; then
echo "Request timed out"
fi
This applies to both connection timeouts and operation timeouts.
Regarding Wget vs cURL, there is a clear usage distinction. cURL supports many protocols (HTTP, FTP, SMTP, etc.) and is often used in scripts and API interactions, while Wget is designed to retrieve files from the web and excel at downloading entire websites.
cURL tends to be better for complex HTTP operations and scripting, while Wget is simpler for straightforward downloads and mirroring websites.
About the author
Augustas Pelakauskas
Senior Copywriter
Augustas Pelakauskas is a Senior Copywriter at Oxylabs. Coming from an artistic background, he is deeply invested in various creative ventures - the most recent one being writing. After testing his abilities in the field of freelance journalism, he transitioned to tech content creation. When at ease, he enjoys sunny outdoors and active recreation. As it turns out, his bicycle is his fourth best friend.
All information on Oxylabs Blog is provided on an "as is" basis and for informational purposes only. We make no representation and disclaim all liability with respect to your use of any information contained on Oxylabs Blog or any third-party websites that may be linked therein. Before engaging in scraping activities of any kind you should consult your legal advisors and carefully read the particular website's terms of service or receive a scraping license.
Get the latest news from data gathering world
Scale up your business with Oxylabs®