· Gists  · 3 min read

Getting your headers using cURL without overthinking

"Was it a capital S I had to pass in?"

"Was it a capital S I had to pass in?"

Inspecting HTTP response headers with cURL is something I find myself doing pretty often. Here’s a clean, reliable way to do it:

curl -sS -D - https://example.com -o /dev/null

What do the flags mean:

-sS: Combines two options:
    -s (silent mode): Suppresses progress and errors.
    -S (show error): Re-enables error output if something goes wrong.

-D -: Dumps the response headers to stdout.
-o /dev/null: Discards the response body.

Add It To Your Shell

function headers() {
  curl -sS -D - "$1" -o /dev/null
}

Now you can just run: headers https://example.com

Automate It with an Installer Script

You can install this as a command-line utility:

Tip

Make sure to chmod +x the script before running it.

#!/bin/bash

# Define colors for output
GREEN='\033[0;32m'
BLUE='\033[0;34m'
RED='\033[0;31m'
NC='\033[0m' # No Color

# Function to print messages
print_step() {
    echo -e "${BLUE}[STEP]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# Check if curl is installed
if ! command -v curl &> /dev/null; then
    print_error "curl is not installed. Please install curl and try again."
    exit 1
fi

# Determine where to place the command
if [[ -d "$HOME/bin" && ":$PATH:" == *":$HOME/bin:"* ]]; then
    # ~/bin exists and is in PATH
    INSTALL_DIR="$HOME/bin"
elif [[ -d "$HOME/.local/bin" && ":$PATH:" == *":$HOME/.local/bin:"* ]]; then
    # ~/.local/bin exists and is in PATH
    INSTALL_DIR="$HOME/.local/bin"
else
    # Create ~/bin if it doesn't exist
    INSTALL_DIR="$HOME/bin"
    mkdir -p "$INSTALL_DIR"

    # Check if we need to update PATH
    if [[ ":$PATH:" != *":$HOME/bin:"* ]]; then
        print_step "Adding $INSTALL_DIR to your PATH..."

        # Determine which shell config file to use
        if [[ -f "$HOME/.bashrc" ]]; then
            SHELL_CONFIG="$HOME/.bashrc"
        elif [[ -f "$HOME/.bash_profile" ]]; then
            SHELL_CONFIG="$HOME/.bash_profile"
        elif [[ -f "$HOME/.zshrc" ]]; then
            SHELL_CONFIG="$HOME/.zshrc"
        else
            print_error "Could not find a shell configuration file (.bashrc, .bash_profile, or .zshrc)."
            print_error "Please manually add $INSTALL_DIR to your PATH."
            exit 1
        fi

        # Add to PATH in shell config
        echo -e "\n# Added by headers command installer" >> "$SHELL_CONFIG"
        echo "export PATH=\"\$HOME/bin:\$PATH\"" >> "$SHELL_CONFIG"

        print_success "Added $INSTALL_DIR to PATH in $SHELL_CONFIG"
        print_step "Please run 'source $SHELL_CONFIG' or restart your terminal for changes to take effect."
    fi
fi

# Create the headers script
HEADERS_SCRIPT="$INSTALL_DIR/headers"

print_step "Creating headers command at $HEADERS_SCRIPT..."

cat > "$HEADERS_SCRIPT" << 'EOF'
#!/bin/bash
# headers - Show HTTP headers for a URL
# Usage: headers [URL]

if [ -z "$1" ]; then
    echo "Usage: headers [URL]"
    echo "Example: headers https://example.com"
    exit 1
fi

curl -sS -D - "$1" -o /dev/null
EOF

# Make it executable
chmod +x "$HEADERS_SCRIPT"

print_success "Created headers command successfully!"
print_step "You can now use the command by typing: headers https://example.com"

# Check if we need to remind about PATH
if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
    print_step "Remember to run 'source $SHELL_CONFIG' or restart your terminal to update your PATH."
fi

# Test the command if possible
if [[ ":$PATH:" == *":$INSTALL_DIR:"* ]]; then
    print_step "Testing headers command..."
    echo "Example output from: headers https://example.com"
    echo "----------------------"
    "$HEADERS_SCRIPT" https://example.com
    echo "----------------------"
fi

print_success "Installation complete!"

[Top]

Share:
Back to Blog