Skip to content
TabBench

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.

Open the cURL to Fetch ConverterFree, no sign-up, and your file never leaves your browser.

Step by step

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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?

Usually CORS: a browser enforces cross-origin rules that curl ignores. The server must send Access-Control-Allow-Origin for your site. Other causes are forbidden headers being dropped, cookies not being sent without credentials: 'include', or a redirect curl did not follow.

How do I send JSON with fetch?

Set the header Content-Type: application/json and pass body: JSON.stringify(object). The converter does this automatically when the curl command has a JSON content type.

What replaces curl -u for Basic auth?

An Authorization header with the value Basic followed by the Base64 of user:password, built with btoa. The converter writes it for you.