Curl is a versatile command line tool for transferring data using various network protocols. One of its features is the ability to set timeouts, ensuring that commands don't run indefinitely if a server is unresponsive. This can be particularly useful when dealing with unreliable networks or services that may experience latency. This tutorial explains how to set timeout with a Curl.
Connection timeout
The connection timeout specifies how long Curl should wait to establish a connection to the server before giving up. This can be controlled using the --connect-timeout
option.
curl --connect-timeout 0.3 https://httpbin.org/get
In this example, Curl will wait up to 0.3 seconds to establish a connection. If the connection isn't established within this time, Curl will terminate the attempt and return an error:
curl: (28) SSL/TLS connection timeout
Maximum Time
The maximum time option limits the total time allowed for the entire operation. This includes DNS resolution, connection time, data transfer time, etc. This can be controlled using the --max-time
option.
curl --max-time 0.6 https://httpbin.org/get
In this example, Curl will wait up to 0.6 seconds on the entire process, including connecting, receiving the response, etc. If the operation isn't completed within this time, Curl will abort and return an error:
curl: (28) Operation timed out after 613 milliseconds with 0 bytes received
Leave a Comment
Cancel reply