curl for HTTP requests in Linux CLI - Time & Space Complexity
When using curl to make HTTP requests, it is helpful to understand how the time it takes grows as you make more requests or handle larger responses.
We want to know how the total work changes when the number or size of requests changes.
Analyze the time complexity of the following curl command used in a script.
for url in $(cat urls.txt); do
curl -s "$url" -o /dev/null
echo "Fetched $url"
done
This script reads a list of URLs from a file and fetches each one silently, discarding the output.
Look for repeated actions that take time.
- Primary operation: The
curlcommand fetching each URL. - How many times: Once for each URL in the list.
As the number of URLs grows, the total time grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 HTTP requests |
| 100 | 100 HTTP requests |
| 1000 | 1000 HTTP requests |
Pattern observation: Doubling the number of URLs doubles the total requests and time.
Time Complexity: O(n)
This means the total time grows linearly with the number of URLs you fetch.
[X] Wrong: "Fetching multiple URLs with curl happens instantly or all at once."
[OK] Correct: Each curl call waits for the server response before moving on, so time adds up with each request.
Understanding how repeated commands like curl scale helps you write efficient scripts and shows you can think about performance in real tasks.
What if we used parallel curl requests instead of one after another? How would the time complexity change?
Practice
curl command primarily do in Linux?Solution
Step 1: Understand the purpose of curl
The curl command is designed to send HTTP requests and interact with web servers.Step 2: Compare with other options
Compiling code, managing permissions, and monitoring processes are unrelated to curl's function.Final Answer:
Send HTTP requests to websites -> Option BQuick Check:
curl = HTTP requests [OK]
- Confusing curl with system commands like chmod or ps
- Thinking curl compiles code
- Assuming curl manages files locally
http://example.com using curl?Solution
Step 1: Identify basic curl usage
To fetch a webpage, simply usecurl URL. No extra flags are needed for a simple GET request.Step 2: Analyze other options
-xis for proxy,--headfetches headers only,-dsends data (POST), so they are incorrect for simple fetch.Final Answer:
curl http://example.com -> Option AQuick Check:
Basic fetch = curl URL [OK]
- Using -x which is for proxy
- Using -d which sends data instead of fetching
- Using --head which fetches headers only
curl -I https://example.com
Solution
Step 1: Understand the -I option in curl
The-Iflag tells curl to fetch only the HTTP headers, not the full content.Step 2: Check other options
The full HTML content of the page is full content, which -I does not do. An error because -I is invalid is wrong because -I is valid. The page content saved to a file requires -o flag, not present here.Final Answer:
Only the HTTP headers of the response -> Option CQuick Check:
-I = headers only [OK]
- Thinking -I fetches full page
- Confusing -I with -o for output file
- Assuming -I is invalid flag
{"name":"John"} to http://api.example.com/users using curl. Which command has an error?Solution
Step 1: Check JSON data format
curl -d '{name:John}' -X POST -H "Content-Type: application/json" http://api.example.com/users uses '{name:John}' which misses double quotes around keys and values, making it invalid JSON. Options A, C, D use correct JSON '{"name":"John"}'.Step 2: Confirm curl flags order
Order of flags does not matter in curl, so A, C, D are valid. Only data format in B is wrong.Final Answer:
curl -d '{name:John}' -X POST -H "Content-Type: application/json" http://api.example.com/users -> Option AQuick Check:
JSON must have quotes [OK]
- Omitting quotes in JSON data
- Thinking flag order matters in curl
- Using wrong content-type header
http://example.com/file.zip and save it as myfile.zip. Which curl command does this correctly?Solution
Step 1: Understand curl file saving options
-o filenamesaves the download to the specified filename.-Osaves with original name from URL.Step 2: Analyze each option
B: curl -O http://example.com/file.zip > myfile.zip saves to 'file.zip' via -O and redirects empty stdout to myfile.zip. C: curl -O myfile.zip http://example.com/file.zip uses -O incorrectly, treating myfile.zip as URL. D: uses invalid flag --save.Final Answer:
curl -o myfile.zip http://example.com/file.zip -> Option DQuick Check:
-o sets output filename [OK]
- Using > redirection for binary files
- Confusing -O and -o flags
- Using non-existent flags like --save
Ready to prove it?
Pass the test and this pattern moves unknown
