Bird
Raised Fist0
Bash Scriptingscripting~20 mins

API interaction scripts in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
API Scripting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
What is the output of this curl command?
You run this command to get JSON data from a public API:

curl -s https://api.agify.io?name=michael | jq '.age'

What will be the output?
Bash Scripting
curl -s https://api.agify.io?name=michael | jq '.age'
AReturns an error because jq is not installed
BReturns the entire JSON response as a string
CReturns the predicted age as a number, e.g., 69
DReturns the name field from the JSON
Attempts:
2 left
💡 Hint
jq extracts specific fields from JSON output.
📝 Syntax
intermediate
1:30remaining
Which curl command correctly sends JSON data via POST?
You want to send JSON data '{"name":"Alice"}' to an API endpoint using curl. Which command is correct?
Acurl -X POST -d '{"name":"Alice"}' https://example.com/api
Bcurl -X POST -H 'Content-Type: application/json' -d '{"name":"Alice"}' https://example.com/api
Ccurl -X POST -H 'Content-Type: text/plain' -d '{"name":"Alice"}' https://example.com/api
Dcurl -X GET -H 'Content-Type: application/json' -d '{"name":"Alice"}' https://example.com/api
Attempts:
2 left
💡 Hint
APIs expect the Content-Type header to match the data format.
🔧 Debug
advanced
2:00remaining
Why does this bash script fail to parse JSON correctly?
This script tries to get the 'token' field from an API response:

response=$(curl -s https://example.com/api/login)
token=$(echo "$response" | jq '.token')


But token is empty. Why?
Bash Scripting
response=$(curl -s https://example.com/api/login)
token=$(echo "$response" | jq '.token')
ABecause curl needs -X POST to get a token
BBecause jq cannot parse JSON from curl output
CBecause the API does not return a token field
DBecause $response is not quoted, so echo splits it and jq gets invalid input
Attempts:
2 left
💡 Hint
Think about how bash handles variables with spaces or newlines.
🚀 Application
advanced
2:30remaining
How to extract all user IDs from a paginated API using bash?
An API returns user data in pages. Each page has a 'users' array and a 'next_page' URL or null if last page.

Write a bash approach to collect all user IDs from all pages.
AUse a loop that fetches each page URL, extracts user IDs, appends them, and follows 'next_page' until null
BFetch only the first page and extract user IDs, ignoring pagination
CUse curl with -L to follow redirects and get all pages automatically
DUse jq to merge all pages by calling the API once with a large limit parameter
Attempts:
2 left
💡 Hint
Pagination requires multiple requests following the next page links.
🧠 Conceptual
expert
2:00remaining
What is the main risk of using curl with user input in a bash script?
You write a bash script that takes a URL from user input and runs:

curl "$user_url"

What is the biggest security risk here?
AUser input could contain malicious commands causing command injection
Bcurl might download large files and fill disk space
Ccurl might fail if the URL is malformed
DUser input might cause curl to connect to unsafe servers
Attempts:
2 left
💡 Hint
Think about how bash interprets variables and special characters.

Practice

(1/5)
1. What is the primary tool used in bash scripts to interact with web APIs?
easy
A. sed
B. grep
C. awk
D. curl

Solution

  1. Step 1: Understand the purpose of each tool

    curl is used to send HTTP requests, while grep, awk, and sed are text processing tools.
  2. Step 2: Identify the tool for API interaction

    Since APIs communicate over HTTP, curl is the correct tool to send requests and receive responses.
  3. Final Answer:

    curl -> Option D
  4. Quick Check:

    API calls use curl [OK]
Hint: Remember: curl sends web requests in bash [OK]
Common Mistakes:
  • Confusing text tools like grep with API tools
  • Using sed or awk for HTTP requests
  • Thinking bash has a built-in HTTP client
2. Which of the following is the correct syntax to send a GET request to https://api.example.com/data using curl?
easy
A. curl https://api.example.com/data
B. curl --data https://api.example.com/data
C. curl -d https://api.example.com/data
D. curl -X POST https://api.example.com/data

Solution

  1. Step 1: Understand default curl behavior

    By default, curl sends a GET request if no method is specified.
  2. Step 2: Analyze options

    curl https://api.example.com/data sends a GET request simply by specifying the URL. The other options either specify POST or data, which changes the method.
  3. Final Answer:

    curl https://api.example.com/data -> Option A
  4. Quick Check:

    Default curl = GET request [OK]
Hint: No method means GET by default in curl [OK]
Common Mistakes:
  • Using -X POST when GET is needed
  • Adding -d or --data without POST method
  • Confusing GET and POST syntax
3. What will be the output of this script snippet?
response=$(curl -s -H "Accept: application/json" https://api.example.com/status)
echo "$response"
medium
A. An error message about missing POST data
B. The raw JSON response from the API
C. Nothing, because the script has a syntax error
D. The HTTP status code only

Solution

  1. Step 1: Understand the command

    The curl command uses -s for silent mode and sets the header to accept JSON. It fetches data from the API URL.
  2. Step 2: Analyze the output

    The response variable stores the full API response body, which is expected to be JSON. The echo prints this JSON string.
  3. Final Answer:

    The raw JSON response from the API -> Option B
  4. Quick Check:

    Silent curl with JSON header returns JSON [OK]
Hint: Silent curl (-s) returns response body only [OK]
Common Mistakes:
  • Expecting HTTP status code instead of body
  • Thinking -H sets request method
  • Assuming syntax error due to quotes
4. Identify the error in this script that sends JSON data to an API:
curl -X POST https://api.example.com/update -d '{"name":"John"}' -H 'Content-Type: application/json'
medium
A. The JSON data is not properly escaped
B. The -X POST option is missing
C. The Content-Type header should be text/plain
D. The URL is missing the protocol

Solution

  1. Step 1: Check JSON data quoting

    The JSON string uses double quotes inside single quotes, but the inner double quotes are escaped with backslashes, which is unnecessary and can cause errors in bash.
  2. Step 2: Correct quoting for JSON in bash

    Use single quotes around JSON without escaping double quotes, or use double quotes outside and escape inner quotes properly.
  3. Final Answer:

    The JSON data is not properly escaped -> Option A
  4. Quick Check:

    Proper JSON quoting avoids escape errors [OK]
Hint: Use single quotes around JSON to avoid escaping double quotes [OK]
Common Mistakes:
  • Over-escaping quotes inside JSON
  • Wrong Content-Type header
  • Omitting -X POST when sending data
5. You want to write a bash script that sends a POST request with JSON data and saves the HTTP status code to a variable. Which command correctly does this?
hard
A. status=$(curl -X POST https://api.example.com/api -d '{"key":"value"}' -H 'Content-Type: application/json')
B. status=$(curl -X POST -H 'Content-Type: application/json' https://api.example.com/api -d '{"key":"value"}')
C. status=$(curl -X POST -d '{"key":"value"}' -H 'Content-Type: application/json' https://api.example.com/api -w "%{http_code}" -o /dev/null -s)
D. status=$(curl -X POST -d '{"key":"value"}' https://api.example.com/api -H 'Content-Type: application/json' -i)

Solution

  1. Step 1: Understand how to capture HTTP status code

    The -w "%{http_code}" option tells curl to output the HTTP status code after the response. The -o /dev/null discards the response body, and -s silences progress.
  2. Step 2: Check command correctness

    status=$(curl -X POST -d '{"key":"value"}' -H 'Content-Type: application/json' https://api.example.com/api -w "%{http_code}" -o /dev/null -s) correctly uses all these options and assigns the status code to the variable. Other options either capture the response body or include headers, not just the status code.
  3. Final Answer:

    status=$(curl -X POST -d '{"key":"value"}' -H 'Content-Type: application/json' https://api.example.com/api -w "%{http_code}" -o /dev/null -s) -> Option C
  4. Quick Check:

    Use -w with -o /dev/null to get status code [OK]
Hint: Use curl -w with -o /dev/null to get only status code [OK]
Common Mistakes:
  • Capturing full response instead of status code
  • Missing -w option for status code
  • Not silencing output with -s
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