API interaction scripts let you talk to other programs or services automatically. This helps you get or send data without doing it by hand.
API interaction scripts in Bash Scripting
curl [options] URL
curl is a command-line tool to send requests to APIs.
You can use options like -X to set the request method (GET, POST), and -d to send data.
curl https://api.example.com/data
curl -X POST -H "Content-Type: application/json" -d '{"name":"John"}' https://api.example.com/users
curl -H "Authorization: Bearer TOKEN" https://api.example.com/secure-dataThis script calls a weather API to get the current temperature for London. It uses curl to send the request and jq to read the temperature from the JSON response.
#!/bin/bash # Simple script to get current weather from a public API API_URL="https://api.weatherapi.com/v1/current.json" API_KEY="your_api_key_here" CITY="London" response=$(curl -s "${API_URL}?key=${API_KEY}&q=${CITY}") # Extract temperature using jq (JSON parser) temp_c=$(echo "$response" | jq '.current.temp_c') echo "The current temperature in $CITY is ${temp_c}°C."
Make sure you have curl installed on your system.
For JSON responses, jq is very helpful to extract data easily.
Always keep your API keys secret and do not share them publicly.
API scripts automate talking to web services to get or send data.
curl is the main tool to send requests in bash scripts.
Use headers and data options to customize your API calls.
Practice
Solution
Step 1: Understand the purpose of each tool
curlis used to send HTTP requests, whilegrep,awk, andsedare text processing tools.Step 2: Identify the tool for API interaction
Since APIs communicate over HTTP,curlis the correct tool to send requests and receive responses.Final Answer:
curl-> Option DQuick Check:
API calls usecurl[OK]
curl sends web requests in bash [OK]- Confusing text tools like grep with API tools
- Using sed or awk for HTTP requests
- Thinking bash has a built-in HTTP client
https://api.example.com/data using curl?Solution
Step 1: Understand default
By default,curlbehaviorcurlsends 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 AQuick Check:
Defaultcurl= GET request [OK]
- Using -X POST when GET is needed
- Adding -d or --data without POST method
- Confusing GET and POST syntax
response=$(curl -s -H "Accept: application/json" https://api.example.com/status) echo "$response"
Solution
Step 1: Understand the command
Thecurlcommand uses-sfor 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. Theechoprints this JSON string.Final Answer:
The raw JSON response from the API -> Option BQuick Check:
Silent curl with JSON header returns JSON [OK]
- Expecting HTTP status code instead of body
- Thinking -H sets request method
- Assuming syntax error due to quotes
curl -X POST https://api.example.com/update -d '{"name":"John"}' -H 'Content-Type: application/json'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 AQuick Check:
Proper JSON quoting avoids escape errors [OK]
- Over-escaping quotes inside JSON
- Wrong Content-Type header
- Omitting -X POST when sending data
Solution
Step 1: Understand how to capture HTTP status code
The-w "%{http_code}"option tellscurlto output the HTTP status code after the response. The-o /dev/nulldiscards the response body, and-ssilences 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 CQuick Check:
Use-wwith-o /dev/nullto get status code [OK]
- Capturing full response instead of status code
- Missing -w option for status code
- Not silencing output with -s
Ready to prove it?
Pass the test and this pattern moves unknown
