How to Extract Only the HTTP Status Code from curl Command Response


1 views

When debugging APIs or checking server responses, we often only need the HTTP status code without all the header noise. The standard curl -I command gives us too much information:

curl -I http://example.com/api
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 15 Aug 2022 14:23:01 GMT
Content-Type: application/json
Connection: keep-alive

Here are three professional approaches to get just the status code:

# Method 1: Using --write-out
curl -o /dev/null -s -w "%{http_code}\n" http://example.com

# Method 2: Parsing with awk
curl -I http://example.com 2>/dev/null | head -n 1 | awk '{print $2}'

# Method 3: Combining with jq (for JSON APIs)
curl -s http://example.com/api | jq -r '.status // .code // "200"'

For scripting and automation, consider these robust implementations:

#!/bin/bash

# Function that returns only status code
get_status() {
    local url=$1
    local status=$(curl -o /dev/null -s -w "%{http_code}" "$url")
    echo $status
}

# Usage example
API_STATUS=$(get_status "https://api.example.com/health")
if [ "$API_STATUS" -ne 200 ]; then
    echo "API is down! Status: $API_STATUS"
    exit 1
fi

For complete reliability, handle these scenarios:

# Follow redirects and get final status
curl -o /dev/null -s -w "%{http_code}" -L http://example.com/redirect

# Get both status and time taken
curl -o /dev/null -s -w "Status: %{http_code}\nTime: %{time_total}s\n" http://example.com

# Handle connection failures
curl -o /dev/null -s -w "%{http_code}" http://example.com || echo "000"

For complex requirements:

# Get status code + specific header
curl -o /dev/null -s -w "%{http_code} %{content_type}\n" http://example.com

# Batch process URLs from file
while read url; do
    echo "$url: $(curl -o /dev/null -s -w "%{http_code}" "$url")"
done < urls.txt

When working with API testing or server monitoring scripts, we often need to quickly check HTTP status codes without processing full response headers. The default curl -I or curl --head command outputs all header information, which can be too verbose for automated scripts.

The most efficient way to get just the status code is combining these curl flags:

curl -s -o /dev/null -w "%{http_code}" http://example.com

Let's break down what each parameter does:

  • -s: Silent mode (no progress meter or errors)
  • -o /dev/null: Redirects output to null
  • -w "%{http_code}": Writes just the HTTP status code

Basic status check:

status=$(curl -s -o /dev/null -w "%{http_code}" https://api.example.com)
echo $status # Outputs: 200

With error handling:

status=$(curl -s -o /dev/null -w "%{http_code}" https://nonexistent.url || echo "000")
if [ "$status" -eq 200 ]; then
    echo "Server is healthy"
else
    echo "Server returned $status"
fi

For those preferring other tools:

# Using wget
wget --spider --server-response http://example.com 2>&1 | awk '/^  HTTP/{print $2}' | tail -1

# Using HTTPie (modern alternative to curl)
http --print=h HEAD http://example.com | grep HTTP | cut -d' ' -f2
  • CI/CD pipeline health checks
  • Automated monitoring scripts
  • API response validation
  • Load balancer backend checks
  • Webhook endpoint verification

When running these checks in loops:

# Add --connect-timeout to prevent hanging
curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 http://example.com

# For HTTP/2 servers, add --http2
curl -s -o /dev/null -w "%{http_code}" --http2 https://example.com