0
0
Bash Scriptingscripting~20 mins

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

Choose your learning style9 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.