SkillEnglish

Testing HTTP APIs with curl: headers, JSON, auth, timing and retries

Practical curl recipes for exercising REST APIs from the shell: JSON bodies, auth headers, file uploads, status checks, timing breakdowns, retries and safe handling of secrets.

Download SKILL.md Raw file (Markdown)

Install in Claude Code

Put the file in a folder named after the skill inside Claude Code's skills folder, then start a new session. This command does it:

bash
mkdir -p ~/.claude/skills/curl-api-testing && \
  curl -fsSL https://md2.dev/library/raw/curl-api-testing.md \
  -o ~/.claude/skills/curl-api-testing/SKILL.md

Preview

Testing HTTP APIs with curl

When to use

Calling or smoke-testing an endpoint, reproducing a bug report, checking headers, CORS, rate limits or latency after a deploy.

BASH
curl --version | head -1        # 7.82+ for --json, 7.76+ for --fail-with-body

Workflow

  1. Start with the simplest request and read the status line and headers (-i).
  2. Add the body and auth one piece at a time.
  3. Use -sS for scripts (silent but still show errors) and --fail-with-body so HTTP errors give a non-zero exit while keeping the message.
  4. Pipe JSON through jq; save binary output with -o.
  5. Record the exact command that reproduces the problem.

Core recipes

BASH
curl -sS https://api.example.com/v1/health | jq .
curl -i https://api.example.com/v1/health                 # status line + headers + body
curl -sI https://api.example.com/                         # headers only (HEAD)

# JSON body (curl 7.82+ sets Content-Type and Accept for you)
curl -sS --json '{"name":"test","tags":["a","b"]}' https://api.example.com/v1/items

# older curl / explicit form
curl -sS -X POST https://api.example.com/v1/items \
  -H 'Content-Type: application/json' -d @body.json

# build the body safely with jq
jq -n --arg t "$TITLE" '{title: $t}' | curl -sS --json @- https://api.example.com/v1/items

# multipart upload
curl -sS -F 'file=@report.pdf;type=application/pdf' -F 'name=Q3' https://api.example.com/upload

# query string with encoding
curl -sG https://api.example.com/search --data-urlencode 'q=تقرير سنوي' -d limit=5

Auth

BASH
curl -sS -H "Authorization: Bearer $API_TOKEN" https://api.example.com/v1/me
curl -sS -u "$USER:$PASS" https://api.example.com/private          # basic auth
curl -sS -H @headers.txt https://api.example.com/v1/me             # headers from a file (7.55+)
curl -sS -b cookies.txt -c cookies.txt https://app.example.com/    # cookie jar

Keep tokens in environment variables or a protected file, never inline in shared commands, and do not paste response bodies that contain them.

Failures, timeouts, retries

BASH
curl -sS --fail-with-body --max-time 30 --connect-timeout 5 https://api.example.com/v1/items
curl -sS --retry 3 --retry-delay 2 --retry-all-errors https://api.example.com/v1/items
curl -sS -o /dev/null -w '%{http_code}\n' https://api.example.com/v1/items   # status code only
curl -sSL -o page.html https://example.com/old-url                          # follow redirects

Timing breakdown

BASH
curl -sS -o /dev/null -w 'dns=%{time_namelookup} connect=%{time_connect} tls=%{time_appconnect} ttfb=%{time_starttransfer} total=%{time_total} size=%{size_download} code=%{http_code}\n' https://api.example.com/

Run it three times: the first call includes cold caches and TLS setup.

Binary responses and headers of interest

BASH
curl -sS -D headers.txt -o out.pdf --json @req.json https://api.example.com/v1/convert
grep -iE '^(content-type|content-disposition|x-ratelimit|retry-after|x-.*cache)' headers.txt
file out.pdf

CORS preflight

BASH
curl -si -X OPTIONS https://api.example.com/v1/items \
  -H 'Origin: https://app.example.com' \
  -H 'Access-Control-Request-Method: POST' \
  -H 'Access-Control-Request-Headers: content-type,authorization' | head -15

Look for access-control-allow-origin, -methods and -headers in the answer.

Gotchas

  • Without --fail-with-body or a status check, curl exits 0 on 4xx and 5xx.
  • Single quotes protect JSON from the shell; for variables use jq -n --arg.
  • -d implies POST and form encoding; --data-binary keeps newlines; -X GET -d is a smell.
  • A 301 after a POST may be turned into GET by -L; use --post301 or call the final URL.
  • On HTTP 429 wait for the Retry-After header before retrying.

Verify

Confirm the status code, the content-type, and that the body parses: jq -e . resp.json. For files check size and type (file, ls -l).

Back to Skills