Command linemediumBash

Curl HTTP Requests and API Testing

Practical curl examples for HTTP requests, API testing, and web debugging.

01

The problem

You need to test APIs, download files, or debug web requests from command line.

02

The solution

Bash
# Basic GET request
curl https://api.example.com/data

# Save output to file
curl -o output.json https://api.example.com/data
curl -O https://example.com/file.zip           # Keep original filename

# Follow redirects
curl -L https://short.url

# Include headers in response
curl -i https://api.example.com/endpoint

# Show request headers only
curl -I https://example.com
curl --head https://example.com

# Verbose output (see full request/response)
curl -v https://api.example.com

# Send POST request with JSON
curl -X POST https://api.example.com/users \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer token123" \
  -d '{"name": "John", "email": "[email protected]"}'

# Send POST with form data
curl -X POST https://example.com/login \
  -d "username=admin" \
  -d "password=secret"

# Send file as multipart/form-data
curl -X POST https://example.com/upload \
  -F "[email protected]" \
  -F "description=Quarterly report"

# Send PUT request
curl -X PUT https://api.example.com/resource/1 \
  -H "Content-Type: application/json" \
  -d '{"status": "updated"}'

# Send DELETE request
curl -X DELETE https://api.example.com/resource/1

# Use custom User-Agent
curl -A "MyBot/1.0" https://example.com

# Add cookies
curl -b "session=abc123" https://example.com/dashboard
curl -b cookies.txt -c cookies.txt https://example.com/login

# Use proxy
curl -x http://proxy:8080 https://example.com

# Limit transfer rate
curl --limit-rate 100K https://example.com/largefile.iso

# Resume interrupted download
curl -C - -O https://example.com/bigfile.zip

# Ignore SSL certificate errors (not for production)
curl -k https://self-signed.example.com

# Use specific SSL/TLS version
curl --tlsv1.2 https://secure.example.com

# Measure request time
curl -w "%{time_total}\n" -o /dev/null -s https://example.com

# Download with authentication
curl -u username:password https://protected.example.com
curl -u username https://protected.example.com  # Prompt for password

# Stream data (e.g., Twitter API)
curl -N https://stream.example.com/events

# Parallel downloads
curl -Z -O https://example.com/file1.zip -O https://example.com/file2.zip

# Use SOCKS proxy
curl --socks5 localhost:9050 https://example.com

03

Put it to work

Example
# Test REST API
curl -X GET https://jsonplaceholder.typicode.com/posts/1

# Create new resource
curl -X POST https://jsonplaceholder.typicode.com/posts \
  -H "Content-Type: application/json" \
  -d '{"title": "Test", "body": "Content", "userId": 1}'

# Download multiple files
curl -O https://example.com/images/[1-10].jpg

# Monitor website response time
while true; do
  curl -w "Time: %{time_total}s\n" -o /dev/null -s https://google.com
  sleep 5
done

# Save cookies and reuse
curl -c cookies.txt https://example.com/login -d "user=me&pass=secret"
curl -b cookies.txt https://example.com/dashboard

Worth knowing

Use -H for headers, -d for data. -X specifies method. Combine -v for debugging. -k bypasses SSL verification (insecure). Use jq with curl for JSON parsing.