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.

WhatCommand
Get a pagecurl https://example.com
Only the response headerscurl -I https://example.com
Headers and bodycurl -i https://example.com
Full conversation (TLS, request, response)curl -v https://example.com
Follow redirectscurl -L http://example.com
Silent, but show errorscurl -sS URL
Fail on HTTP errors (exit code ≠ 0)curl -f URL
Only the status codecurl -s -o /dev/null -w "%{http_code}\n" URL

Downloads

WhatCommand
Save under the remote namecurl -LO https://example.com/file.tar.gz
Save under your own namecurl -L -o app.tar.gz URL
Resume a broken downloadcurl -C - -LO URL
Limit speedcurl --limit-rate 2M -LO URL

wget URL is the simpler alternative for plain downloads (wget -c resumes).

APIs

WhatCommand
POST JSONcurl -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 filecurl --json @data.json URL
PUT / DELETEcurl -X PUT ... / curl -X DELETE URL
HTML formcurl -d "user=anna&pw=x" URL
Upload a filecurl -F "file=@backup.tar.gz" URL
Add a headercurl -H "Accept: application/json" URL
Bearer tokencurl -H "Authorization: Bearer $TOKEN" URL
Basic auth (asks for password)curl -u anna URL
Pretty-print JSON responsecurl -s URL | jq (see jq)

Keep tokens out of your shell history: put them in a variable with read -s TOKEN first.

Testing servers

WhatCommand
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 headercurl -H "Host: example.com" http://10.0.0.5
Ignore certificate errors (test only!)curl -k https://10.0.0.5
Use a custom CAcurl --cacert ca.pem https://intranet
Client certificatecurl --cert client.pem --key client.key URL
Through a proxycurl -x http://proxy:3128 URL
Force IPv4 / IPv6curl -4 URL / curl -6 URL
Timeout after 5 scurl --max-time 5 URL
Retry 3 timescurl --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
§ home
~/knubertus/home/
1,1 Top