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.
التثبيت في Claude Code
ضع الملف في مجلد باسم المهارة داخل مجلد skills الخاص بـ Claude Code، ثم ابدأ جلسة جديدة. الأمر التالي يفعل ذلك:
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 المعاينة
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.
curl --version | head -1 # 7.82+ for --json, 7.76+ for --fail-with-bodyWorkflow
- Start with the simplest request and read the status line and headers (
-i). - Add the body and auth one piece at a time.
- Use
-sSfor scripts (silent but still show errors) and--fail-with-bodyso HTTP errors give a non-zero exit while keeping the message. - Pipe JSON through
jq; save binary output with-o. - Record the exact command that reproduces the problem.
Core recipes
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=5Auth
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 jarKeep 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
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 redirectsTiming breakdown
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
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.pdfCORS preflight
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 -15Look for access-control-allow-origin, -methods and -headers in the answer.
Gotchas
- Without
--fail-with-bodyor a status check, curl exits 0 on 4xx and 5xx. - Single quotes protect JSON from the shell; for variables use
jq -n --arg. -dimplies POST and form encoding;--data-binarykeeps newlines;-X GET -dis a smell.- A
301after aPOSTmay be turned intoGETby-L; use--post301or call the final URL. - On HTTP 429 wait for the
Retry-Afterheader 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).