Overview
curl is the standard command-line tool for transferring data over HTTP, HTTPS, FTP, and dozens of other protocols. Developers use it to test APIs, inspect headers, and automate downloads. This cheat sheet collects the most useful invocations.
Basic Requests
# GET request
curl https://api.example.com/users
# Save response to a file
curl -o users.json https://api.example.com/users
# Silent mode with error output only
curl -s https://api.example.com/health
HTTP Methods
| Method | Command |
|---|---|
| GET | curl https://api.example.com/items |
| POST | curl -X POST https://api.example.com/items |
| PUT | curl -X PUT https://api.example.com/items/1 |
| PATCH | curl -X PATCH https://api.example.com/items/1 |
| DELETE | curl -X DELETE https://api.example.com/items/1 |
Sending Data
# JSON body
curl -X POST https://api.example.com/users \
-H "Content-Type: application/json" \
-d '{"name":"Alice","email":"alice@example.com"}'
# Form data
curl -X POST https://api.example.com/upload \
-F "file=@photo.jpg" \
-F "caption=Holiday"
# URL-encoded form
curl -X POST https://api.example.com/login \
-d "username=alice&password=secret"
Headers and Authentication
# Custom header
curl -H "Accept: application/json" https://api.example.com
# Bearer token
curl -H "Authorization: Bearer $TOKEN" https://api.example.com/me
# Basic auth
curl -u user:password https://api.example.com/private
# Read token from environment
curl -H "Authorization: Bearer ${API_KEY}" https://api.example.com
Inspecting the Response
| Flag | Effect |
|---|---|
-i | Include response headers |
-I | Fetch headers only (HEAD request) |
-v | Verbose output including request headers |
-w "%{http_code}" | Print only the status code |
--fail | Return non-zero exit code on HTTP errors |
curl -s -o /dev/null -w "%{http_code}\n" https://example.com
Following Redirects
curl -L https://example.com/redirect
Without -L, curl stops at the first 3xx response and prints the redirect body.
Timeouts and Retries
curl --connect-timeout 5 --max-time 30 https://api.example.com
curl --retry 3 --retry-delay 2 https://api.example.com
Downloading Files
# Save with the remote filename
curl -O https://example.com/file.zip
# Resume a partial download
curl -C - -O https://example.com/large.iso
# Show a progress bar
curl --progress-bar -O https://example.com/file.zip
Working with Cookies
# Save cookies
curl -c cookies.txt https://example.com/login
# Send stored cookies
curl -b cookies.txt https://example.com/dashboard 