cURL to JavaScript

Convert a curl command to JavaScript fetch: real output, a form body example, errors and CORS.

loading cURL to JavaScript
About this tool

cURL to JavaScript: from curl to code

Paste a curl command and get the JavaScript that sends the same request with fetch, which runs in every browser and in Node.js 18 or later with no dependency. The method, headers and body are copied into the options object; a JSON body is written as JSON.stringify of an object. The tool also writes Axios and Node versions, which you can switch to in its language list.

This page opens the converter on JavaScript fetch. The command is only read and turned into text: nothing is run and nothing is sent from the page.

A real conversion

The tool opens with this command, a POST of a JSON order with a bearer token:

curl -X POST 'https://api.example.com/v1/orders?expand=items' \
  -H 'Authorization: Bearer sk_test_4eC39HqLyjWDarjtT1zdp7dc' \
  -H 'Content-Type: application/json' \
  -d '{"customer":"ana@example.com","items":[{"sku":"KB-01","qty":2}],"coupon":null}'

and this is what it writes for JavaScript fetch:

const response = await fetch("https://api.example.com/v1/orders?expand=items", {
  method: "POST",
  headers: {
    "Authorization": "Bearer sk_test_4eC39HqLyjWDarjtT1zdp7dc",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "customer": "ana@example.com",
    "items": [
      {
        "sku": "KB-01",
        "qty": 2
      }
    ],
    "coupon": null
  }),
  redirect: "manual",
});

console.log(response.status);
console.log(await response.text());

Which flags become what

Starting from a plain GET of https://api.example.com/items, each flag below adds this to the generated code (computed with the converter itself):

FlagWhat it isFirst new line
-H 'Accept: application/json'a request headerheaders: {
-d 'a=1&b=2'a body (the method becomes POST)method: "POST",
-u ana:secretbasic authenticationheaders: {
-F 'photo=@a.png'a multipart file uploadconst form = new FormData();
-X DELETEanother methodmethod: "DELETE",

Another command

curl -X POST https://api.example.com/login -d 'user=ana&pass=1234' --max-time 10 -L
const response = await fetch("https://api.example.com/login", {
  method: "POST",
  headers: {
    "Content-Type": "application/x-www-form-urlencoded",
  },
  body: "user=ana&pass=1234",
});

console.log(response.status);
console.log(await response.text());

A form body keeps its application/x-www-form-urlencoded header and is sent as a string. With -L the generated code follows redirects, which is fetch's default.

Using it in a page or in Node

// In a browser console, or a .mjs file with Node 18+
const response = await fetch(url, options);
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const data = await response.json();

Common mistakes

  • Expecting fetch to reject on 404 or 500: it only rejects on network failure. Check response.ok or response.status, as in the snippet.
  • Running the call from a web page of another origin: the browser applies CORS and may block it even though curl worked. Call it from a server, or ask the API to allow your origin.

How to use the converter

  1. Copy the command: from the browser (network tab, Copy as cURL), from an API reference or from your terminal history.
  2. Paste it in the box; lines that end with a backslash are joined, so a multi-line command works.
  3. Copy the code or download it as a file.

Frequently asked questions

How do I convert curl to fetch?

Paste the command and copy the fetch call; the URL, method, headers and body are carried over, and the language list has Axios and Node versions of the same request.

What does "Copy as cURL" in the browser give me?

A curl command with every header the browser sent, cookies included. Paste it here to get the request as code, then remove the headers you do not need (cookies, sec-fetch-*).

fetch, Axios or Node http?

fetch needs no dependency and works everywhere; Axios adds interceptors and rejects on error statuses; the Node version is for older Node without fetch.

Report a problem