Bird
Raised Fist0
Bash Scriptingscripting~20 mins

HTTP requests with curl in scripts in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Curl Command Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this curl command?
You run this command in a terminal to get the HTTP status code of a website:

curl -o /dev/null -s -w "%{http_code}" https://example.com

What will this command output?
Bash Scripting
curl -o /dev/null -s -w "%{http_code}" https://example.com
AThe full HTML content of https://example.com
B200
CAn error message about missing output file
DNothing (blank output)
Attempts:
2 left
💡 Hint
The flags -o /dev/null and -s hide the content and progress, -w "%{http_code}" prints the status code.
💻 Command Output
intermediate
2:00remaining
What does this curl command output?
This command sends a POST request with JSON data:

curl -X POST -H "Content-Type: application/json" -d '{"name":"Alice"}' https://httpbin.org/post

What kind of output will you get?
Bash Scripting
curl -X POST -H "Content-Type: application/json" -d '{"name":"Alice"}' https://httpbin.org/post
ANo output, command runs silently
BThe HTML page of httpbin.org homepage
CA JSON response showing the data sent and request details
DAn error about unsupported HTTP method
Attempts:
2 left
💡 Hint
httpbin.org/post echoes back the POST data in JSON format.
🔧 Debug
advanced
2:00remaining
Why does this curl command fail with a certificate error?
You run this command:

curl https://self-signed.badssl.com/

It fails with an SSL certificate error. Which option fixes this error by ignoring certificate validation?
Bash Scripting
curl https://self-signed.badssl.com/
Acurl --insecure https://self-signed.badssl.com/
Bcurl --no-ssl https://self-signed.badssl.com/
Ccurl --ignore-certs https://self-signed.badssl.com/
Dcurl --skip-verify https://self-signed.badssl.com/
Attempts:
2 left
💡 Hint
The option to ignore SSL certificate errors is --insecure or -k.
🚀 Application
advanced
2:00remaining
How to save HTTP response headers to a file using curl?
You want to save only the HTTP response headers from a GET request to https://example.com into a file named headers.txt. Which command does this correctly?
Acurl -D headers.txt https://example.com -o /dev/null
Bcurl -H headers.txt https://example.com
Ccurl -I https://example.com > headers.txt
Dcurl --headers https://example.com > headers.txt
Attempts:
2 left
💡 Hint
The -D option saves headers to a file; -o /dev/null discards the body.
🧠 Conceptual
expert
2:00remaining
What is the effect of this curl command in a script?
Consider this bash script snippet:

response=$(curl -s -w "%{http_code}" -o response.txt https://example.com)

What will be stored in the variable response after running this command?
Bash Scripting
response=$(curl -s -w "%{http_code}" -o response.txt https://example.com)
AAn empty string
BThe full HTML content of https://example.com
CThe path to the saved file response.txt
DThe HTTP status code as a string, e.g., "200"
Attempts:
2 left
💡 Hint
The -w option outputs the status code to stdout, -o saves body to file, -s silences progress.

Practice

(1/5)
1. What does the curl command do in a bash script?
easy
A. It creates a new directory on the system.
B. It sends HTTP requests to websites or APIs.
C. It compiles the bash script into an executable.
D. It displays the current date and time.

Solution

  1. 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.
  2. Step 2: Match curl's function to the options

    Among the options, only sending HTTP requests matches curl's purpose.
  3. Final Answer:

    It sends HTTP requests to websites or APIs. -> Option B
  4. Quick Check:

    curl = HTTP requests [OK]
Hint: Remember curl talks to websites using HTTP [OK]
Common Mistakes:
  • Confusing curl with system commands like mkdir or date
  • Thinking curl compiles scripts
  • Assuming curl only downloads files
2. Which of the following is the correct syntax to send a GET request with curl in a bash script?
easy
A. curl -send https://example.com
B. curl --post https://example.com
C. curl -X GET https://example.com
D. curl get https://example.com

Solution

  1. Step 1: Identify the correct curl option for GET

    The -X GET option explicitly tells curl to use the GET method, which is the default.
  2. Step 2: Check other options for correctness

    Options B, C, and D use invalid or incorrect flags for curl.
  3. Final Answer:

    curl -X GET https://example.com -> Option C
  4. Quick Check:

    GET request syntax = curl -X GET [OK]
Hint: Use -X GET to specify GET method explicitly [OK]
Common Mistakes:
  • Using --post instead of -X POST for POST requests
  • Typing invalid flags like -send or get
  • Omitting the URL
3. What will the following script output?
response=$(curl -s -o /dev/null -w "%{http_code}" https://httpbin.org/status/200)
echo $response
medium
A. 200
B. OK
C. 404
D. curl: command not found

Solution

  1. Step 1: Understand curl options used

    The -s option silences progress, -o /dev/null discards output, and -w "%{http_code}" prints the HTTP status code.
  2. Step 2: Analyze the URL and expected status

    The URL https://httpbin.org/status/200 returns HTTP status 200, so the variable response will hold "200".
  3. Final Answer:

    200 -> Option A
  4. Quick Check:

    HTTP status code = 200 [OK]
Hint: Use -w "%{http_code}" to get HTTP status code [OK]
Common Mistakes:
  • Expecting full page content instead of status code
  • Missing quotes around %{http_code}
  • Not using -s to silence output
4. Identify the error in this script snippet:
curl -X POST -d 'name=John' https://example.com/api
if [ $? -eq 0 ]; then
  echo "Success"
else
  echo "Failed"
fi
medium
A. No error; script works correctly
B. Missing quotes around URL
C. Single quotes prevent variable expansion in -d
D. Using -X POST with -d is invalid

Solution

  1. Step 1: Check curl command syntax

    The command uses -X POST with -d 'name=John' correctly to send POST data.
  2. Step 2: Verify script logic for success check

    The script checks the exit status $? properly and echoes success or failure accordingly.
  3. Final Answer:

    No error; script works correctly -> Option A
  4. Quick Check:

    Script syntax and logic = correct [OK]
Hint: Check exit status $? after curl for success [OK]
Common Mistakes:
  • 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
5. You want to write a bash script that sends a JSON POST request with curl to https://api.example.com/data and saves the response to response.json. Which script snippet correctly does this?
hard
A. curl -X POST -H 'Content-Type: application/json' -d '{key:value}' https://api.example.com/data >> response.json
B. curl -POST -H 'Content-Type: application/json' -d '{key:value}' https://api.example.com/data > response.json
C. curl -X POST -H Content-Type application/json -d '{"key":"value"}' https://api.example.com/data > response.json
D. curl -X POST -H 'Content-Type: application/json' -d '{"key":"value"}' https://api.example.com/data > response.json

Solution

  1. 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 POST and sets the header -H 'Content-Type: application/json' with quotes.
  2. 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 overwrite response.json.
  3. 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 -POST flag 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.
  4. Final Answer:

    curl -X POST -H 'Content-Type: application/json' -d '{"key":"value"}' https://api.example.com/data > response.json -> Option D
  5. Quick 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]
Hint: Use -H for headers, -d with valid JSON, > to save output [OK]
Common Mistakes:
  • Forgetting quotes around JSON keys and values
  • Using invalid curl flags like -POST
  • Appending instead of overwriting output file
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard