18 / 55 · 19 Linux & Command Line · grep, curl, and jq← prev⊞ allnext →☰ Read as one page
3.3curl -- Make HTTP Requests
curl sends HTTP requests from the command line. It is invaluable for quick API testing, health checks, and debugging network issues.
Basic Requests
# GET request
curl https://api.example.com/users
# Pretty-print JSON response (pipe through jq)
curl -s https://api.example.com/users | jq .
# POST with JSON body
curl -X POST https://api.example.com/users \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d '{"name": "Test User", "email": "test@example.com"}'
# PUT request
curl -X PUT https://api.example.com/users/123 \
-H "Content-Type: application/json" \
-d '{"name": "Updated User"}'
# DELETE request
curl -X DELETE https://api.example.com/users/123 \
-H "Authorization: Bearer $TOKEN"
Inspecting Responses
# Response headers only (HEAD request)
curl -I https://api.example.com/users
# HTTP/2 200
# content-type: application/json
# cache-control: no-cache
# Verbose output (debug connection issues)
curl -v https://api.example.com/users
# Shows: DNS resolution, TCP connection, TLS handshake, request headers, response headers
# Show response headers along with body
curl -i https://api.example.com/users
# Only show the HTTP status code
curl -s -o /dev/null -w "%{http_code}" https://api.example.com/users
# Output: 200
Timing Requests
# Time the request (useful for performance testing)
curl -w "\nTime: %{time_total}s\nHTTP Code: %{http_code}\n" \
-o /dev/null -s https://api.example.com/users
# Output:
# Time: 0.245s
# HTTP Code: 200
# Detailed timing breakdown
curl -w "\nDNS: %{time_namelookup}s\nConnect: %{time_connect}s\nTLS: %{time_appconnect}s\nFirst byte: %{time_starttransfer}s\nTotal: %{time_total}s\n" \
-o /dev/null -s https://api.example.com/users
Practical curl Scenarios
# Upload a file
curl -X POST https://api.example.com/upload \
-F "file=@screenshot.png" \
-H "Authorization: Bearer $TOKEN"
# Follow redirects
curl -L https://example.com/old-url
# Save response to a file
curl -o response.json https://api.example.com/users
# Send a request with cookies
curl -b "session=abc123" https://api.example.com/profile
# Set a timeout (prevent hanging)
curl --max-time 30 https://slow-api.example.com/endpoint
# Retry on failure
curl --retry 3 --retry-delay 5 https://flaky-api.example.com/endpoint