Convert cURL to JavaScript Fetch

Paste a curl command — for example from the browser's developer tools, “Copy as cURL” — and get the equivalent fetch() call, with async/await or promise chains, for a web page or for Node.js.

Loading tool…

About the cURL to Fetch

The command is read the way a shell reads it: single and double quotes, backslash escapes, ANSI-C strings ($'…', used by Chrome and Firefox in “Copy as cURL” for bodies with line breaks or quotes), line continuations, and the ^ escapes of “Copy as cURL (cmd)” on Windows. The options are then interpreted like curl does: -d makes the request a POST with the content type application/x-www-form-urlencoded, several -d are joined with &, --data-urlencode percent-encodes its value, -G moves the data into the query string, --json sets the Content-Type and Accept headers, -F builds a multipart form, -u becomes HTTP Basic authentication and -I a HEAD request.

The generated code is meant to be read and kept. A JSON body is written as JSON.stringify({ … }) with an object literal when that produces the same data, a form body as URLSearchParams when it encodes to the same bytes, and otherwise as a string with correct escaping — template literals for text with several lines. Multipart forms become FormData; files that curl reads from disk (@file) become a clearly marked placeholder in the browser and fs.readFileSync() in Node.js. --max-time becomes AbortSignal.timeout().

A browser does not let a page set every header. Cookie, Host, Referer, Origin, Content-Length, Accept-Encoding and all Sec- and Proxy- headers are forbidden header names in the Fetch standard and are silently ignored; they are kept in the code with a comment so you can see what the original request sent. Other differences from curl are listed under the code: fetch() follows redirects by default while curl needs -L, -k and -x cannot be reproduced in a page, and requests to another origin are subject to CORS. The conversion runs in your browser — tokens and cookies in the command are not uploaded.

How to use it

  1. In the browser's developer tools open the Network tab, right-click a request and choose Copy → Copy as cURL. Or paste any curl command.
  2. Choose the target: fetch for the browser or for Node.js, or axios, XMLHttpRequest, Python requests.
  3. Choose async/await or promise .then() style.
  4. Read the warnings and notes under the code, then copy or download it.

Frequently asked questions

Why does the fetch request fail with a CORS error when curl works?
curl is not a browser, so the same-origin policy does not apply to it. A page may only read a response from another origin when the server allows it with Access-Control-Allow-Origin, and custom headers or a JSON content type trigger a preflight OPTIONS request the server must answer. Run the code in Node.js, or call the API from your own server.
Why are the Cookie and User-Agent headers ignored in the browser?
They are forbidden or browser-controlled header names: the browser sends the cookies it has stored for the site (with credentials: "include" for other origins) and its own user agent. In Node.js, fetch sends these headers as written.
Does fetch follow redirects like curl -L?
Yes, by default (redirect: "follow"), up to 20 redirects. curl only follows redirects with -L. Use redirect: "manual" or "error" to change that.
Is my command sent to a server?
No. Parsing and code generation happen in JavaScript in your browser, so API keys, bearer tokens and cookies in the command stay on your machine.

Related tools