---
name: curl-api-testing
description: 'Test and debug HTTP APIs with curl: send JSON and multipart requests, set auth headers, fail on HTTP errors, inspect headers, measure latency with --write-out, retry flaky endpoints, save binary responses, avoid leaking secrets. Use when the user wants to call or smoke-test an endpoint, reproduce an API bug, check CORS or rate limits, or verify a deployment. Triggers: curl request، test api، http status، post json، measure response time.'
---

# 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`).
