HTTP requests with curl in scripts in Bash Scripting - Time & Space Complexity
When running HTTP requests in a script, it is important to understand how the time to complete grows as you make more requests.
We want to know how the script's running time changes when the number of curl requests increases.
Analyze the time complexity of the following code snippet.
for url in "$@"; do
curl -s "$url" > /dev/null
echo "Fetched $url"
done
This script takes a list of URLs and fetches each one using curl, printing a message after each fetch.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that runs curl for each URL.
- How many times: Once for each URL passed to the script.
Each additional URL adds one more curl request, so the total time grows directly with the number of URLs.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 curl requests |
| 100 | 100 curl requests |
| 1000 | 1000 curl requests |
Pattern observation: The time grows linearly as you add more URLs.
Time Complexity: O(n)
This means the total time increases in direct proportion to the number of URLs you fetch.
[X] Wrong: "Running multiple curl commands in a loop happens instantly or all at once."
[OK] Correct: Each curl command waits for the server response before moving on, so the time adds up with each request.
Understanding how loops with network calls grow helps you write efficient scripts and explain your reasoning clearly in interviews.
"What if we ran multiple curl requests in parallel instead of one after another? How would the time complexity change?"
Practice
curl command do in a bash script?Solution
Step 1: Understand the purpose of curl
The curl command is designed to transfer data from or to a server using various protocols, mainly HTTP.Step 2: Match curl's function to the options
Among the options, only sending HTTP requests matches curl's purpose.Final Answer:
It sends HTTP requests to websites or APIs. -> Option BQuick Check:
curl = HTTP requests [OK]
- Confusing curl with system commands like mkdir or date
- Thinking curl compiles scripts
- Assuming curl only downloads files
Solution
Step 1: Identify the correct curl option for GET
The-X GEToption explicitly tells curl to use the GET method, which is the default.Step 2: Check other options for correctness
Options B, C, and D use invalid or incorrect flags for curl.Final Answer:
curl -X GET https://example.com -> Option CQuick Check:
GET request syntax = curl -X GET [OK]
- Using --post instead of -X POST for POST requests
- Typing invalid flags like -send or get
- Omitting the URL
response=$(curl -s -o /dev/null -w "%{http_code}" https://httpbin.org/status/200)
echo $responseSolution
Step 1: Understand curl options used
The-soption silences progress,-o /dev/nulldiscards output, and-w "%{http_code}"prints the HTTP status code.Step 2: Analyze the URL and expected status
The URL https://httpbin.org/status/200 returns HTTP status 200, so the variableresponsewill hold "200".Final Answer:
200 -> Option AQuick Check:
HTTP status code = 200 [OK]
- Expecting full page content instead of status code
- Missing quotes around %{http_code}
- Not using -s to silence output
curl -X POST -d 'name=John' https://example.com/api if [ $? -eq 0 ]; then echo "Success" else echo "Failed" fi
Solution
Step 1: Check curl command syntax
The command uses-X POSTwith-d 'name=John'correctly to send POST data.Step 2: Verify script logic for success check
The script checks the exit status$?properly and echoes success or failure accordingly.Final Answer:
No error; script works correctly -> Option AQuick Check:
Script syntax and logic = correct [OK]
- Thinking single quotes block variable expansion here (no variables used)
- Assuming -X POST and -d can't be combined
- Forgetting to check curl exit status
https://api.example.com/data and saves the response to response.json. Which script snippet correctly does this?Solution
Step 1: Check HTTP method and headers
curl -X POST -H 'Content-Type: application/json' -d '{"key":"value"}' https://api.example.com/data > response.json correctly uses-X POSTand sets the header-H 'Content-Type: application/json'with quotes.Step 2: Validate JSON data and output redirection
curl -X POST -H 'Content-Type: application/json' -d '{"key":"value"}' https://api.example.com/data > response.json uses valid JSON with escaped quotes and redirects output with>to overwriteresponse.json.Step 3: Identify errors in other options
curl -POST -H 'Content-Type: application/json' -d '{key:value}' https://api.example.com/data > response.json uses invalid-POSTflag and unquoted JSON keys. curl -X POST -H Content-Type application/json -d '{"key":"value"}' https://api.example.com/data > response.json misses quotes around header value. curl -X POST -H 'Content-Type: application/json' -d '{key:value}' https://api.example.com/data >> response.json appends with>>and has invalid JSON keys.Final Answer:
curl -X POST -H 'Content-Type: application/json' -d '{"key":"value"}' https://api.example.com/data > response.json -> Option DQuick Check:
Correct POST JSON syntax and output redirection = curl -X POST -H 'Content-Type: application/json' -d '{"key":"value"}' https://api.example.com/data > response.json [OK]
- Forgetting quotes around JSON keys and values
- Using invalid curl flags like -POST
- Appending instead of overwriting output file
Ready to prove it?
Pass the test and this pattern moves unknown
