The essentials

Quick reference

One focused task per row. Jump to the related section for complete, working examples.

UseSyntaxExamples
Include response headerscurl --include https://example.com/healthView examples
Request headers onlycurl --head https://example.com/assetView examples
Trace protocol exchangecurl --verbose https://example.com/healthView examples
Follow redirectscurl --location --max-redirs 5 https://example.com/oldView examples
Fail on HTTP errorscurl --fail-with-body https://example.com/apiView examples
Print status metadatacurl --silent --output /dev/null --write-out \ '%{http_code}\n' https://example.com/View examples
Send JSONcurl --json @payload.json https://api.example.com/itemsView examples
Add a headercurl --header 'Accept: application/json' \ https://api.example.com/itemsView examples
Upload a file bodycurl --upload-file ./artifact.bin \ https://upload.example.com/artifact.binView examples
Bound timecurl --connect-timeout 5 --max-time 30 \ https://example.com/View examples
Retry transient failurescurl --retry 3 --retry-all-errors --max-time 30 \ https://example.com/View examples
Save to an exact pathcurl --fail --output ./artifact.tar.gz \ https://example.com/artifact.tar.gzView examples
Use an explicit CA bundlecurl --cacert ./ca.pem https://internal.example/View examples
Measure phasescurl --output /dev/null --write-out \ 'dns=%{time_namelookup} connect=%{time_connect} total=%{time_total}\n' \ https://example.com/View examples

curl is both a transfer client and a precise HTTP diagnostic tool. Keep secrets out of command history and process arguments, distinguish response bodies from diagnostics, fail automation on HTTP errors, bound connection and total time, and verify certificates rather than disabling TLS checks.

Step by step

Detailed examples

01

Separate body output from diagnostic output

curl writes response bodies to stdout and verbose diagnostics to stderr, so redirect them independently. --include prints response headers with the body; --head sends HEAD, which servers may implement differently from GET. Verbose output can expose authorization headers and cookies.

Capture headers and body separately
curl --silent --show-error \
  --dump-header ./response.headers \
  --output ./response.body \
  https://example.com/health
sed -n '1,20p' ./response.headers
Back to quick reference ↑
02

Make redirect and status policy explicit

curl normally treats HTTP error status as a successful transfer and does not follow redirects. --fail-with-body changes 400+ into a nonzero result, while --location follows redirects. Credentials can cross trust boundaries, so review redirect targets and avoid broad credential forwarding.

Bound redirects and fail automation
curl --silent --show-error --fail-with-body \
  --location --max-redirs 5 \
  --output ./response.json \
  https://api.example.com/v1/status
Back to quick reference ↑
03

Let curl construct content headers where possible

--json sends data and defaults Content-Type and Accept to JSON. @file reads a file and @- reads stdin. Prefer config files, stdin, netrc with protected permissions, or external credential helpers over secrets directly in arguments; arguments may appear in history and process listings.

Post a reviewed JSON file
curl --silent --show-error --fail-with-body \
  --json @payload.json \
  --output ./created.json \
  https://api.example.com/items
Back to quick reference ↑
04

Bound waits and retry only safe operations

Connection timeout covers establishment; max-time bounds an attempt. Retries can duplicate non-idempotent operations if the server processed a request before failure. Use retry policy primarily for idempotent reads or endpoints with idempotency keys, and cap total orchestration time outside curl as needed.

Bound an idempotent health request
curl --silent --show-error --fail \
  --connect-timeout 5 --max-time 20 \
  --retry 3 --retry-delay 1 \
  https://example.com/health
Back to quick reference ↑
05

Verify transport and artifact identity

TLS verification authenticates the endpoint against trusted CAs; --insecure disables that protection and should not become a workaround. A successful HTTPS download still needs an expected hash or signature when artifact integrity matters. Save to a staging path and verify before replacing live files.

Download and verify a published digest
curl --fail --location --output ./artifact.tar.gz https://example.com/artifact.tar.gz
printf '%s  %s\n' 'EXPECTED_SHA256' './artifact.tar.gz' | sha256sum --check -

Note: Replace EXPECTED_SHA256 with a value obtained through the publisher's trusted release channel.

Back to quick reference ↑
06

Measure phases before diagnosing the network

write-out exposes DNS, connection, TLS, first-byte, and total timing. Proxy environment variables can silently change the route; inspect them without printing embedded secrets. A single request is not a benchmark, but phase timings help locate name resolution, connection, server, or transfer delays.

Report request phases
curl --silent --show-error --output /dev/null \
  --write-out 'remote=%{remote_ip} dns=%{time_namelookup} connect=%{time_connect} tls=%{time_appconnect} first=%{time_starttransfer} total=%{time_total}\n' \
  https://example.com/
Back to quick reference ↑

Sources and further reading

References

Authoritative documentation used to verify and expand this cheat sheet.

  1. curl Projectcurl command line tool manualcurl.se
  2. curl Projectcurl HTTP scripting guidecurl.se
  3. curl Projectcurl TLS certificate verificationcurl.se

Help us improve

Found a typo or missing example?

Tell us what would make this cheat sheet clearer, more complete, or more useful.

Share feedback