When it comes to making web requests, timing is crucial. Nobody likes to wait еndlеssly for a wеbpagе to load or a sеrvеr to respond. If you’re using curl to fetch data from the web, you’ll be pleased to know that there are ways to put a cap on the time your requests take.

Here’s how to limit curl request time to make sure that you stay in control of your data-fetching operations.

using maxtime option with curl on ubuntu

Method 1: Using the –max-time Option

Whilerunning the curl command, you can specify the-mor–max-timeoption to set a strict time limit on the request. This option lets you specify the maximum time, in seconds, that you’re willing to let the command take before it exits with a timeout error code (28):

You can define the maximum with decimal precision, where 0.3 means 300 milliseconds, 5.46 equals 5,460 milliseconds, and 20 means 20 seconds.

using connect timeout option with curl on ubuntu

For instance:

This command sends a request to the provided GitHub URL. It will save the response data as test.md and finish within a maximum time of 30 seconds.

Method 2: Using the –connect-timeout Option

To control the time curl spends trying to connect to a host, use the–connect-timeoutoption. This sets a maximum time limit for curl to complete the connection steps, includingDNS lookupand subsequent TCP, TLS, or QUIC handshakes.

If curl cannot establish a connection within the time frame you specify, it will exit with a timeout error code (28):

For example:

Here, the curl command retrieves the file specified in the URL, saves it as test.md, and enforces a 20-second limit for establishing the connection with the server.

Control Curl Requests for Swift Retrieval

In a world where time is money, being able to limit curl request time is a handy skill. Whether you choose the –connect-timeout or –max-time option, you’re taking charge of your data-fetching tasks. So, no more endless waiting, it’s time to make efficient, controlled web requests.

Next time you find yourself waiting for a curl request to finish, remember these methods and make curl work on your terms.