How to convert a curl command to JavaScript fetch
By TabBenchHow we check our guides
API documentation and bug reports almost always show requests as curl commands, but the code you write in a web app or Node.js script uses fetch. Translating by hand is easy to get wrong: -d sends form data unless you add a header, -u becomes a Base64 header, -F builds a multipart body, and a few headers that curl sends freely are forbidden in a browser.
This guide shows the workflow from the browser's Network tab to working fetch code, what each curl flag turns into, and the differences between curl and fetch that make a correct translation fail anyway.
Step by step
Copy the request as cURL
In Chrome, Edge or Firefox, open the developer tools, go to the Network tab, find the request, right-click it and choose Copy, then Copy as cURL (bash). Paste the command into the converter. The same converter also understands Windows cmd and PowerShell formats and commands written by hand.
Map the flags to fetch options
-X sets the method option, -H adds an entry to headers, and -d or --data-raw becomes the body. -u user:pass becomes an Authorization: Basic header. -F fields become a FormData object. -L lets fetch follow redirects, which it does by default anyway. --max-time becomes AbortSignal.timeout in milliseconds. The converter does all of this and writes the body as JSON.stringify of an object when the content type is JSON.
Choose async/await or promises
async/await reads top to bottom and works at the top level of ES modules and in DevTools. The .then() chain works anywhere, including older bundlers. Either form should check response.ok, because fetch only rejects on network failures, not on HTTP errors such as 404 or 500.
Check what a browser will not send
Browsers refuse to set some headers from script, including Cookie, Host, Referer and Origin, and ignore User-Agent. A request copied from DevTools usually contains these, so they are dropped. To send cookies with a cross-origin request use credentials: 'include' and make sure the server allows it. From Node.js, none of these limits apply.
Test it and fix CORS if needed
curl is not a browser, so it never hits CORS rules. If the same request fails from your page with a CORS error, the server must allow your origin with Access-Control-Allow-Origin, and answer the OPTIONS preflight for JSON bodies or custom headers. Test the request first in a terminal or in the API Request Builder to separate a server problem from a browser restriction.
Things worth knowing
- Do not set Content-Type yourself when the body is FormData: the browser adds the multipart boundary.
- curl does not follow redirects unless you pass -L, while fetch does. Set redirect: 'manual' if the difference matters.
- Never commit tokens copied from DevTools. Replace them with environment variables before sharing code.
- Node.js 18 and later include fetch. Older versions need node-fetch or undici.
Frequently asked questions
Why does my curl command work but the fetch version fails?
How do I send JSON with fetch?
What replaces curl -u for Basic auth?
Related tools
cURL to Axios Converter
Convert a curl command to Axios code for JavaScript.
cURL to Python Converter
Convert a curl command to Python requests, httpx or urllib.
cURL to JavaScript Converter
Convert curl to fetch, XMLHttpRequest, Node.js https or jQuery.