BASH
Perform HTTP Request and Inspect Response with cURL
Use cURL within a Bash script to make HTTP GET requests, displaying full response headers and the body, essential for debugging web services and APIs.
#!/bin/bash
# Fetches a URL and displays both response headers and body.
# Usage: ./http_get.sh <URL>
if [ -z "$1" ]; then
echo "Usage: $0 <URL>"
echo "Example: $0 https://api.example.com/data"
exit 1
fi
TARGET_URL="$1"
echo "Fetching $TARGET_URL..."
echo "-------------------- Request Details --------------------"
curl -svo /dev/null "$TARGET_URL" --write-out "
HTTP Status: %{http_code}
Total Time: %{time_total}s
"
echo "-------------------- Response Headers --------------------"
curl -sI "$TARGET_URL"
echo "-------------------- Response Body --------------------"
curl -s "$TARGET_URL"
How it works: This script leverages the `curl` command-line tool to perform an HTTP GET request to a user-specified URL. It provides a comprehensive view of the interaction by first showing verbose request/response information (status code, total time) without saving the body, then separately displaying only the response headers, and finally printing the full response body. This makes it an invaluable tool for web developers to quickly debug API endpoints, check content, and inspect network interactions directly from the terminal.