Converting curl to JavaScript fetch
Published: 2026-07-20
When to translate curl commands to fetch(), which flags map cleanly, and why parsing locally beats pasting API tokens into online converters.
curl is the lingua franca of HTTP debugging. Browser DevTools, OpenAPI docs, and README examples often ship a curl one-liner. Modern apps, however, call APIs with fetch() (browser and Node 18+). Translating between the two saves time when prototyping — but pasting a curl that contains Bearer tokens into a random converter sends your credential to someone else’s server.
What converts cleanly
| curl flag | fetch equivalent |
|---|---|
-X POST |
{ method: 'POST' } |
-H 'Key: Value' |
headers: { Key: 'Value' } |
--data-raw '{…}' with JSON |
body: JSON.stringify({…}) |
-d 'a=b&c=d' |
body: 'a=b&c=d' (often application/x-www-form-urlencoded) |
-F 'field=value' |
URL-encoded form body |
-u user:pass |
Authorization: Basic … header |
-G with -d |
query string appended to URL, GET method |
What does not map 1:1
-k/--insecure— curl skips TLS verification; browsers and secure runtimes cannot mirror that casually.- Client certificates — curl
--certhas no fetch twin; use Nodehttpsagent or mTLS proxy. - Cookie jars — curl
-bsends one header snapshot; browsers manage cookies separately. - HTTP/2 prior knowledge, proxies, unix sockets — out of scope for simple converters.
Always read generated code and adjust for your runtime (Node may need globalThis.fetch or undici).
Typical workflow
- Copy Copy as cURL from Network tab (includes cookies and auth — treat as secret).
- Convert to fetch locally.
- Replace hard-coded tokens with environment variables.
- Delete the curl from tickets and screenshots after debugging.
Try it locally
The curl to fetch converter parses common curl syntax and emits JavaScript fetch() with headers and JSON bodies — entirely in your browser.
Pair with Reading a JWT without trusting the server when your curl carries Authorization: Bearer ….