正在加载,请稍候…

curl and wget: HTTP Debugging and API Testing from CLI

Master curl and wget for HTTP debugging, API testing, and file operations. Learn common patterns for REST APIs, authentication, and troubleshooting.

curl and wget: HTTP Debugging and API Testing from CLI

curl Basics

# GET request
curl https://api.example.com/users

# Verbose output (headers + body)
curl -v https://api.example.com/users

# Show only response headers
curl -I https://api.example.com/users

# Custom headers
curl -H "Authorization: Bearer TOKEN"      -H "Content-Type: application/json"      https://api.example.com/protected

REST API Operations

# POST with JSON
curl -X POST https://api.example.com/users   -H "Content-Type: application/json"   -d '{"name": "Alice", "email": "alice@example.com"}'

# PUT
curl -X PUT https://api.example.com/users/1   -H "Content-Type: application/json"   -d '{"name": "Alice Updated"}'

# DELETE
curl -X DELETE https://api.example.com/users/1

# File upload (multipart)
curl -X POST https://api.example.com/upload   -F "file=@/path/to/file.pdf"   -F "name=document"

Authentication

# Basic auth
curl -u username:password https://api.example.com

# Bearer token
curl -H "Authorization: Bearer eyJhbGci..." https://api.example.com

# API key
curl "https://api.example.com/data?api_key=YOUR_KEY"

Response Handling

# Save response to file
curl -o output.json https://api.example.com/data

# Follow redirects
curl -L https://example.com

# Show status code
curl -o /dev/null -w "%{http_code}" https://api.example.com/health

# Pretty print JSON (with jq)
curl https://api.example.com/users | jq '.[] | {id, name}'

wget for Downloads

# Download file
wget https://example.com/file.zip

# Download with custom filename
wget -O myfile.zip https://example.com/file.zip

# Recursive download
wget -r -l2 https://example.com/docs/

# Resume interrupted download
wget -c https://example.com/largefile.iso

Debugging Tips

# Timing breakdown
curl -o /dev/null -w "
Connect: %{time_connect}s
TTFB: %{time_starttransfer}s
Total: %{time_total}s
" https://api.example.com

# SSL certificate info
curl -v --ssl-no-revoke https://example.com 2>&1 | grep -A5 "Server certificate"

# Proxy debugging
curl -x http://proxy:8080 https://api.example.com

curl is the Swiss Army knife of HTTP debugging for developers.