An API (Application Programming Interface) is a way for programs to talk to each other. In scripting, it lets your script send requests to a service and get data back.
Click to reveal answer
beginner
Which command is commonly used in bash scripts to interact with APIs?
The curl command is commonly used to send HTTP requests to APIs from bash scripts.
Click to reveal answer
intermediate
How do you include headers like API keys in a curl request?
Use the -H option with curl. For example: curl -H "Authorization: Bearer YOUR_API_KEY" URL
Click to reveal answer
beginner
What does the -X POST option do in a curl command?
It tells curl to send a POST request instead of the default GET request. POST is used to send data to the API.
Click to reveal answer
intermediate
Why is it useful to parse API responses in bash scripts?
Parsing API responses lets your script understand the data returned, so it can make decisions or show useful info. Tools like jq help extract data from JSON responses.
Click to reveal answer
Which command sends a GET request to an API in bash?
By default, curl sends a GET request. Option A is a simple GET request.
How do you add a header for an API key in a curl request?
Acurl -H 'Authorization: Bearer YOUR_API_KEY' URL
Bcurl -X GET YOUR_API_KEY URL
Ccurl --api-key YOUR_API_KEY URL
Dcurl -d 'Authorization=YOUR_API_KEY' URL
✗ Incorrect
Headers are added with -H option. Authorization header is common for API keys.
What tool helps parse JSON responses in bash scripts?
Ased
Bgrep
Cawk
Djq
✗ Incorrect
jq is designed to parse and filter JSON data easily.
Which HTTP method is used to send data to an API?
AGET
BDELETE
CPOST
DHEAD
✗ Incorrect
POST is used to send data to an API, often to create or update resources.
What does the -d option do in a curl command?
ADeletes data from the server
BAdds data to send in the request body
CDisplays detailed output
DDownloads a file
✗ Incorrect
-d sends data in the request body, usually with POST requests.
Explain how you would write a bash script to get data from an API that requires an API key.
Think about how to include the API key in the request headers.
You got /4 concepts.
Describe how to handle and extract useful information from a JSON API response in a bash script.
Consider tools that help read JSON in bash.
You got /4 concepts.
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
Step 1: Understand the purpose of each tool
curl is used to send HTTP requests, while grep, awk, and sed are text processing tools.
Step 2: Identify the tool for API interaction
Since APIs communicate over HTTP, curl is the correct tool to send requests and receive responses.
Final Answer:
curl -> Option D
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
Step 1: Understand default curl behavior
By default, curl sends a GET request if no method is specified.
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.
Final Answer:
curl https://api.example.com/data -> Option A
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?
The curl command uses -s for silent mode and sets the header to accept JSON. It fetches data from the API URL.
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.
Final Answer:
The raw JSON response from the API -> Option B
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
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.
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.
Final Answer:
The JSON data is not properly escaped -> Option A
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
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.
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.
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
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 unknownstrongwhile earning +1 Grip toward your certificate.