====== curl ======
===== Requests =====
curl https://api.example.com # GET
curl -X POST # bare POST
curl -X PUT|PATCH|DELETE
curl -d 'a=1&b=2' # POST form data
curl -d @file.json -H 'Content-Type: application/json' # body from file
curl -F 'file=@photo.jpg' -F 'name=x' # multipart upload
curl --data-binary @payload.bin # raw body, no processing
curl -G -d 'q=search' -d 'page=2' # -d as QUERY params
===== Headers & auth =====
curl -H 'Accept: application/json'
curl -H 'Authorization: Bearer TOKEN'
curl -u user:pass # basic auth
curl -u user # prompts for pass
curl --oauth2-bearer TOKEN
curl --netrc # creds from ~/.netrc
===== Downloading =====
curl -O # remote filename
curl -o out.bin # your filename
curl -C - -O # resume partial download
curl --retry 5 --retry-delay 2 --retry-all-errors
curl --limit-rate 1M -O # bandwidth cap (kind to a slow link)
curl -L # follow redirects (usually wanted)
curl -f # fail (nonzero exit) on HTTP errors
===== Inspecting responses =====
curl -I # HEAD request, headers only
curl -i # headers + body
curl -v # verbose: request+TLS+response trace
curl -sS # silent but show errors: script default
curl -D headers.txt # save headers to file
===== `-w`: timing and metadata =====
curl -so /dev/null -w '%{http_code}\n' # just the status
curl -so /dev/null -w '%{time_total}s %{speed_download}B/s\n'
curl -o /dev/null -w '%{size_download} %{http_code} %{url_effective}\n'
Common vars: ''http_code'' ''time_total'' ''time_namelookup'' ''time_connect''
''speed_download'' ''size_download'' ''remote_ip'' ''redirect_url'' ''url_effective''.
===== Cookies & sessions =====
curl -c jar.txt # save cookies
curl -b jar.txt # send them
curl -c jar -b jar # both: keeps a session across calls
curl -b 'name=val' # one-off cookie
===== Tricks =====
curl --resolve host:443:1.2.3.4 https://host/ # bypass DNS
curl --connect-to ::staging:443 https://prod/ # hit staging as prod
curl -A 'Mozilla/5.0' # user-agent
curl --compressed # accept gzip/br
curl -k https://self-signed/ # skip TLS verify (debug only)
curl --cacert cert.pem # custom CA
curl --proxy http://127.0.0.1:8080 # via proxy
curl --noproxy '*' # force direct
===== In scripts =====
code=$(curl -so /dev/null -w '%{http_code}' "$url")
curl -fsSL "$url" || die "download failed" # -f = exit !=0 on HTTP error
body=$(curl -fsS "$api" ) # capture response
echo "$body" | jq '.items[0]'
''~/.curlrc'' for defaults (e.g. ''retry = 3'', ''fail'').
===== Debug a bad response =====
curl -sv 2>&1 | less # whole exchange incl. TLS
curl --trace-ascii - # even more detail
Suspects, in order: DNS (''time_namelookup'' huge / ''Could not resolve''),
TLS handshake, 4xx auth, 5xx their problem. ''curl -w'' splits timing per
phase to isolate where it's slow.
===== See Also =====
* [[terminal:scripting:start|Scripting Index]]
* [[terminal:tools:start|CLI Tools]]
* [[terminal:start|Return to terminal]]
* [[start|Return to wiki home]]