curl to JavaScript Fetch Converter
Convert a curl command into modern JavaScript that uses the Fetch API. The generated snippet keeps the method, the query parameters, the headers and the request body from your original command, so a request you captured in the browser network panel becomes code you can paste straight into a script.
curl command
JavaScript code
Ready — paste some input or load the sample.
100% Client-Side · Privacy First. This tool runs entirely inside your browser. Nothing you type is uploaded, logged or sent to a server — you can even use it offline.
Overview
The browser fetch API is a different shape from curl: there is no command line to parse, the options object is explicit, and the body has to be serialised by you. This converter takes a curl command and produces the JavaScript that does the same thing with fetch. The method, the headers, the body and the credentials are all mapped onto the options object, and a JSON body is wrapped in JSON.stringify so the browser sends the correct content type and the server can parse it. The generated code also handles the failure case: it checks response.ok and throws with the status and the status text, then parses the body as JSON when that is what the server returned. That last part matters, because fetch does not throw on a 404 and a script that forgets to check will happily process an error page. The result is a snippet you can drop into a script, a bookmarklet or a service worker.
How to use the curl to javascript fetch converter
- 1Copy the curl command from the browser devtools network panel, or from your shell history.
- 2Paste it into the input panel.
- 3Read the generated snippet and paste it into your script.
- 4Run it in a browser context, or in Node.js 18 and newer, where fetch and async await are available.
- 5Read the notes panel for the flags that a browser cannot reproduce, such as skipping TLS verification.
- 6Keep the error check, because it is the difference between a handled failure and a silent one.
Cheat sheet
The syntax you will use most, and what each part does.
| Pattern | What it means |
|---|---|
-X PUT |
Becomes method: "PUT" in the options object. |
-H "Accept: application/json" |
Becomes an entry in the headers object. |
-d '{"a":1}' |
With a JSON content type it becomes body: JSON.stringify({ a: 1 }). |
-u user:pass |
Becomes an Authorization header with the base64 encoded credentials. |
-b "k=v" |
Becomes a Cookie header, because fetch has no cookie dictionary option. |
--compressed |
Is accepted and ignored, since a browser always requests compressed content. |
Common mistakes
The body arrives as a string.
A JSON body must be serialised with JSON.stringify and sent with a JSON content type, which the converter does for you.
A 404 is treated as a success.
fetch only rejects on a network failure. The generated code checks response.ok and throws with the status and status text.
Cookies are not sent.
fetch needs the credentials option, and only same-origin requests get cookies by default. Cross-origin calls need credentials: "include" and a matching CORS setup.
The header casing changes.
HTTP header names are case insensitive, so X-Api-Key and x-api-key mean the same thing to a server.
Worked examples
A JSON POST
curl -X POST https://api.example.com/items -H 'Content-Type: application/json' -d '{"name":"x"}'
A snippet that builds the options object, serialises the body and checks the response.
A GET with a bearer token
curl https://api.example.com/me -H 'Authorization: Bearer abc123'
A snippet with the authorization header and a JSON response parse.
Frequently asked questions
Can I run the generated code in Node.js?
Yes. Node.js 18 and newer ship a global fetch, so the snippet runs unchanged with no dependency. On older releases you need to install a polyfill or use the Python converter instead.
How are the credentials handled?
A cookie flag becomes credentials: "include", which lets the browser send cookies for a same-origin request. Basic auth with -u is dropped, because a browser sets those credentials from the session instead.
Why is there an error check in the snippet?
fetch only rejects on a network failure. An HTTP 404 or 500 is a successful request as far as the API is concerned, so the snippet checks response.ok and throws instead of letting you parse an error page as data.
Does the snippet handle redirects?
It follows them by default, the same way curl does without the -L flag, and the browser rewrites the URL for you so the final response is what your code sees.
Why does the body look different from the curl command?
A JSON body is turned into a JavaScript object literal and passed through JSON.stringify, which is safer than pasting a raw string. A form body is kept as a plain string.
Can I use the generated code from a Node.js script on a server?
Yes, but remember that a server-side request has no browser origin, so CORS does not apply while some other limits do. The snippet is deliberately framework free so it drops into any runtime.
What happens to the -k flag?
It is dropped and reported in the notes. A browser cannot be told to ignore an invalid certificate, and silently generating code that disables TLS verification would be dangerous.
Are the header names changed?
They keep the case you typed, because HTTP header names are case insensitive but some servers still match on the exact spelling. The notes remind you of that.
Why does the snippet use async and await?
Because fetch returns a promise. Async and await let the generated code read top to bottom in the same order as the curl command, instead of hiding every call inside a then callback.
What happens to a form encoded body?
It is kept as a plain string, which is exactly what a request with a form content type expects. Only a JSON body is turned into an object, because that is the case where the shape matters.
Can I use the snippet inside a framework?
Yes, the body of the snippet is plain code. Many frameworks expect a returned response object instead of a console log, so replace the last two lines with the return statement your framework uses.