All articles

Copy as cURL: replaying browser requests from DevTools

· 2 min read

When a request fails in the browser but you can't tell why, the fastest way to dig in is to replay it outside the page. Every major browser can copy any network request as a curl command with the same method, URL, headers, cookies and body. You can then run it, edit it one piece at a time and attach it to a bug report.

Copy a request

  1. Open DevTools (F12, or Cmd+Option+I on macOS) and switch to the Network panel.
  2. Reload the page or trigger the action so the request appears.
  3. Right-click the request:
    • Chrome and Edge: Copy > Copy as cURL (bash), or (cmd) on Windows.
    • Firefox: Copy Value > Copy as cURL (POSIX or Windows).
    • Safari: Copy as cURL (enable the Develop menu in Settings > Advanced first).

Filtering the Network panel by Fetch/XHR hides images and scripts so the API call is easier to find.

What you get

A typical copied command looks like this:

curl 'https://api.example.com/v1/orders?status=open' \
  -H 'accept: application/json' \
  -H 'accept-language: en-US,en;q=0.9' \
  -H 'authorization: Bearer eyJhbGciOi...' \
  -H 'cookie: session=abc123; theme=dark' \
  -H 'origin: https://app.example.com' \
  -H 'referer: https://app.example.com/orders' \
  -H 'sec-ch-ua: "Chromium";v="141"' \
  -H 'sec-fetch-mode: cors' \
  -H 'user-agent: Mozilla/5.0 ...' \
  --data-raw '{"page":2}'

--data-raw sends the body as-is and makes curl use POST unless -X says otherwise.

Trim it down

Most headers are added by the browser and don't matter to the server. Removing them makes the command readable and shows which ones do matter. Usually safe to delete first:

  • sec-ch-ua* and sec-fetch-*: client hints and fetch metadata.
  • accept-language, accept-encoding, priority, cache-control, pragma.
  • user-agent, unless the server behaves differently per browser.
  • referer and origin, unless the server checks them (CORS and CSRF protection often do).

Keep authorization, cookie, content-type and any custom x- headers until you've confirmed they aren't needed. Then remove one header at a time: when the response changes, you've found what the server depends on.

Useful curl options while debugging

curl -i   URL   # include response headers
curl -v   URL   # show the full request and response, including TLS
curl -L   URL   # follow redirects
curl -sS  URL   # no progress bar, but still show errors
curl -o /dev/null -w '%{http_code} %{time_total}s\n' -s URL   # status and timing only
curl --compressed URL   # ask for gzip/br and decompress

Pipe JSON responses through jq . to format them, or paste them into a formatter.

Don't leak credentials

A copied command contains everything needed to act as the logged-in user: session cookies, bearer tokens, sometimes CSRF tokens. Before pasting it into a ticket, chat or AI assistant:

  • Replace cookie and authorization values with placeholders like <SESSION> and <TOKEN>.
  • Check the URL and body for API keys or personal data.
  • If a real token was shared, log out or revoke it so it stops working.

From curl to code

Once the request works in the terminal, it often needs to become code: a reproduction script, a test or a new API client. The fetch equivalent of the trimmed command above is:

const response = await fetch("https://api.example.com/v1/orders?status=open", {
  method: "POST",
  headers: {
    "accept": "application/json",
    "authorization": "Bearer <TOKEN>",
    "content-type": "application/json",
  },
  body: JSON.stringify({ page: 2 }),
});
const data = await response.json();

A few curl behaviors don't carry over to fetch:

  • -k / --insecure: fetch can't skip certificate checks.
  • Cookies in browsers: a page can't set the Cookie header in fetch; use credentials: "include" and let the browser send its cookies. Node.js can set the header.
  • Redirects: fetch follows them by default; curl only does with -L.
  • Timeouts: --max-time becomes signal: AbortSignal.timeout(ms).

For a detailed look at sending a JSON body, see the curl POST JSON example. If the replayed request comes back with a 3xx status, the redirect guide explains which ones change the method.

Frequently asked questions

Why does the copied command fail with 401 or 403?

The session cookie or token may have expired, or the server checks something the terminal doesn't send, such as a CSRF token that has since rotated. Copy a fresh request right before replaying it. 401 vs 403 explains what each one tells you.

What's the difference between Copy as cURL (bash) and (cmd)?

Quoting. The bash version uses single quotes for macOS, Linux and Git Bash; the cmd and PowerShell variants escape for Windows shells. Using the wrong one breaks on quotes and special characters.

Can I copy a request as fetch directly?

Yes. Chrome and Firefox also offer Copy as fetch (and Copy as Node.js fetch in Chrome). Copy as cURL is more useful when you want to replay and tweak the request outside the browser first.

Does the copied command include the response?

No, only the request: method, URL, headers and body.

Further reading