FormatterHQ

cURL to JavaScript fetch() Converter

Paste a curl command and get modern JavaScript using fetch() with async/await. JSON bodies, form data, headers, basic auth and timeouts are converted.

Input SHELL
Output JAVASCRIPT
Ready.

How to use the cURL to JavaScript Fetch

  1. Copy a curl command from documentation or from your browser’s developer tools.
  2. Paste it into the input editor.
  3. The fetch() code is generated immediately, including notes for options fetch cannot reproduce.
  4. Copy the code into an async function or a module in your project.

From curl to JavaScript fetch()

The Fetch API is built into every modern browser and into Node.js 18 and later, so it is the natural target when turning curl examples into JavaScript. This curl to JavaScript converter generates clean fetch() code with await, a status check and a response parser, ready to drop into your app.

Example

curl -X POST 'https://api.example.com/v1/orders?notify=true' \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer YOUR_API_TOKEN' \
  -d '{"sku":"LAMP-01","qty":2}' \
  --max-time 30
const response = await fetch('https://api.example.com/v1/orders?notify=true', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    Authorization: 'Bearer YOUR_API_TOKEN',
  },
  body: JSON.stringify({
    "sku": "LAMP-01",
    "qty": 2
  }),
  signal: AbortSignal.timeout(30000),
});

if (!response.ok) {
  throw new Error('HTTP ' + response.status);
}

const data = await response.text();
console.log(data);

A JSON body is detected and written as JSON.stringify(…), which is easier to edit than an escaped string. When the request sends an Accept: application/json header, the code reads the response with response.json() instead of response.text().

How curl features translate

  • Form uploads (-F) become a FormData object. File fields reference fileInput.files[0], since browser code cannot read arbitrary paths from disk.
  • Basic auth (-u) becomes an Authorization: Basic header built with btoa().
  • Timeouts (-m) use AbortSignal.timeout().
  • Redirects – fetch follows redirects by default, like curl -L.

What fetch cannot do

Some curl options have no fetch equivalent, and the converter adds a comment instead of silently dropping them:

  • -k (skip certificate checks) – not possible in fetch; certificate validation stays on.
  • -x (proxy) – browsers do not let pages configure a proxy.
  • Manual Cookie headers – browsers ignore them; use credentials: 'include' for the site's own cookies (a manual header does work in Node.js).

CORS: why it works in curl but not in the browser

curl talks to any server directly. A browser page may only read responses from another origin if that server allows it with CORS headers such as Access-Control-Allow-Origin. If the generated code fails in the browser with a CORS error while curl works, the API is not configured for browser access. Run the code in Node.js or call the API from your own back end instead. Never put secret API keys in front-end code that users can inspect.

Supported curl options

curl optionMeaning
-X, --requestHTTP method (GET, POST, PUT, PATCH, DELETE…)
-H, --headerRequest headers
-d, --data, --data-raw, --data-binary, --data-urlencodeRequest body; implies POST
--jsonJSON body with Content-Type and Accept headers
-F, --formMultipart form fields and file uploads
-u, --user and --oauth2-bearerBasic and bearer authentication
-b, --cookie, -A, -eCookies, user agent and referer
-G, --getSend -d data as query string
-L, -k, -m, -x, -I, -TFollow redirects, skip TLS checks, timeout, proxy, HEAD request, upload file

Line continuations (\), single and double quotes are parsed like a Unix shell. Options that only affect curl's own output, such as -s, -v or -o, are ignored silently; any other unsupported option is listed as a comment at the top of the generated code, so nothing is dropped without notice.

Prefer another language? Try cURL to Python or cURL to PHP. You can also test the request interactively with the API tester. Conversion runs locally in your browser.

Frequently asked questions

Does the generated code work in Node.js?
Yes. Node.js 18 and later include fetch, FormData and AbortSignal.timeout globally. Run the code inside an async function or an ES module that supports top-level await.
Why do I get a CORS error in the browser?
The API does not allow requests from your page’s origin. curl is not subject to CORS, but browsers are. Call the API from a server or ask the provider to enable CORS for your domain.
Can fetch ignore SSL certificate errors like curl -k?
No. Browsers never allow pages to skip certificate validation. The converter adds a comment noting that the -k option was not converted.
Why does the code use response.text() instead of response.json()?
The converter uses json() when the request asks for JSON with an Accept header. Otherwise it reads text, which works for any response; switch to json() if you know the API returns JSON.
Is the curl command sent to a server?
No. It is parsed and converted in your browser, so tokens in the command stay on your device.

Last updated September 26, 2026

Related tools

JavaScript Tester Run JavaScript online and see console.log, warnings, errors and tables instantly in a safe, sandboxed browser console. cURL to Axios (Node.js) Converter Convert curl commands to Node.js axios requests with params, headers, JSON data, auth, form-data uploads and timeouts. cURL to C# Converter Convert curl commands to C# HttpClient code for .NET with headers, content types, basic auth, multipart and TLS options. cURL to Go Converter Convert curl commands into Go code using net/http — method, headers, JSON or form body, basic auth, timeouts and multipart uploads. cURL to PHP Guzzle Converter Convert curl commands to PHP Guzzle requests using json, form_params, multipart, query and auth options. cURL to Java Converter Convert curl commands to Java 11+ java.net.http.HttpClient code with headers, body, redirects and timeouts. cURL to PHP Converter Convert curl commands into PHP code using the curl_* functions: method, headers, body, auth, cookies and SSL options. cURL to Python Requests Convert curl commands into Python requests code: headers, params, JSON or form data, files, cookies, auth and timeouts. cURL to Rust Converter Convert curl commands to Rust using the reqwest crate, with JSON bodies via serde_json, headers, auth and timeouts. Date & Time Calculator Calculate the difference between two dates or add/subtract days, weeks, months and years from a date. Docker Run to Docker Compose Convert one or more docker run commands into a docker-compose.yml: ports, volumes, env, restart, networks, healthchecks and more. File Type Checker Find out what a file really is from its content, not its name. Detects 150+ formats and warns when the extension does not match.