Basic Requests
# GET request
curl https://api.example.com/users
# POST with JSON body
curl -X POST https://api.example.com/users \
-H "Content-Type: application/json" \
-d '{"name":"Alice","email":"alice@example.com"}'
# PUT request
curl -X PUT https://api.example.com/users/1 \
-H "Content-Type: application/json" \
-d '{"name":"Alice Updated"}'
# DELETE request
curl -X DELETE https://api.example.com/users/1
Common Options
| Flag | Description |
-s | Silent (no progress bar) |
-v | Verbose (show headers) |
-o file | Save output to file |
-O | Save with remote filename |
-L | Follow redirects |
-i | Include response headers |
-w "%{http_code}" | Print status code |
--max-time 10 | Timeout in seconds |
Authentication
# Bearer token
curl -H "Authorization: Bearer TOKEN" https://api.example.com
# Basic auth
curl -u username:password https://api.example.com
# API key in header
curl -H "X-API-Key: your-key" https://api.example.com
File Upload
# Upload file
curl -F "file=@photo.jpg" https://api.example.com/upload
# Multiple files
curl -F "file1=@a.jpg" -F "file2=@b.jpg" https://api.example.com/upload
Useful Patterns
# Pretty-print JSON response
curl -s https://api.example.com/data | jq .
# Save response and check status
STATUS=$(curl -s -o response.json -w "%{http_code}" https://api.example.com)
echo "Status: $STATUS"
# POST form data
curl -X POST -d "user=alice&pass=secret" https://example.com/login