ubuntu, updated
curl cheat sheet
Test websites and APIs from the shell: headers, status codes, POST requests, authentication, downloads, TLS and timing.
The basics
curl URL sends a request and prints the response body. Everything else is options.
| What | Command |
|---|---|
| Get a page | curl https://example.com |
| Only the response headers | curl -I https://example.com |
| Headers and body | curl -i https://example.com |
| Full conversation (TLS, request, response) | curl -v https://example.com |
| Follow redirects | curl -L http://example.com |
| Silent, but show errors | curl -sS URL |
| Fail on HTTP errors (exit code ≠ 0) | curl -f URL |
| Only the status code | curl -s -o /dev/null -w "%{http_code}\n" URL |
Downloads
| What | Command |
|---|---|
| Save under the remote name | curl -LO https://example.com/file.tar.gz |
| Save under your own name | curl -L -o app.tar.gz URL |
| Resume a broken download | curl -C - -LO URL |
| Limit speed | curl --limit-rate 2M -LO URL |
wget URL is the simpler alternative for plain downloads (wget -c resumes).
APIs
| What | Command |
|---|---|
| POST JSON | curl -X POST -H "Content-Type: application/json" -d '{"name":"web01"}' URL |
| POST JSON (short, curl 7.82+) | curl --json '{"name":"web01"}' URL |
| JSON from a file | curl --json @data.json URL |
| PUT / DELETE | curl -X PUT ... / curl -X DELETE URL |
| HTML form | curl -d "user=anna&pw=x" URL |
| Upload a file | curl -F "file=@backup.tar.gz" URL |
| Add a header | curl -H "Accept: application/json" URL |
| Bearer token | curl -H "Authorization: Bearer $TOKEN" URL |
| Basic auth (asks for password) | curl -u anna URL |
| Pretty-print JSON response | curl -s URL | jq (see jq) |
Keep tokens out of your shell history: put them in a variable with read -s TOKEN first.
Testing servers
| What | Command |
|---|---|
| Ask a specific IP, keep the hostname (bypass DNS/load balancer) | curl --resolve example.com:443:10.0.0.5 https://example.com |
| Send a different Host header | curl -H "Host: example.com" http://10.0.0.5 |
| Ignore certificate errors (test only!) | curl -k https://10.0.0.5 |
| Use a custom CA | curl --cacert ca.pem https://intranet |
| Client certificate | curl --cert client.pem --key client.key URL |
| Through a proxy | curl -x http://proxy:3128 URL |
| Force IPv4 / IPv6 | curl -4 URL / curl -6 URL |
| Timeout after 5 s | curl --max-time 5 URL |
| Retry 3 times | curl --retry 3 URL |
Where does the time go?
curl -s -o /dev/null -w "dns: %{time_namelookup}s connect: %{time_connect}s tls: %{time_appconnect}s first byte: %{time_starttransfer}s total: %{time_total}s\n" https://example.com
Slow dns → DNS problem. Slow connect → network. Slow first byte → the application.
Show the certificate
curl -vI https://example.com 2>&1 | grep -E "subject:|issuer:|expire date:"
Health check in a script
if curl -fsS --max-time 5 http://localhost:8080/health > /dev/null; then
echo "OK"
else
echo "DOWN"; exit 1
fi