UnblockDevs

How to Convert cURL to JavaScript fetch, Axios & Node.js — Complete Guide

API docs, Chrome DevTools, and Postman all export requests as cURL — but your frontend or Node.js backend needs JavaScript. This guide covers how to convert every cURL pattern to fetch(), Axios, and Node.js https module: GET, POST with JSON, auth headers, form data, file uploads, and error handling. Plus the free online converter that does it instantly.

fetch()

Built into every modern browser and Node.js 18+

axios

Most popular JS HTTP library — 50M+ npm downloads/week

1 paste

Convert any cURL to JS with the online converter

1

fetch() vs Axios — Which to Use When?

Before converting cURL to JavaScript, choose which library fits your context. Both can do everything cURL does — the differences are mostly ergonomic.

Use fetch() when…

You are in a browser or Node.js 18+ without adding dependencies. fetch() is built-in, needs no install, and works for basic GET/POST with JSON. The API is slightly verbose for error handling but fine for simple cases.

Use Axios when…

You want automatic JSON parsing, request/response interceptors, cleaner error handling, timeout support, upload progress, or need to support older Node.js versions. npm install axios once, then use everywhere.

Use node-fetch when…

You need fetch-like API on Node.js 14/16 without the full Axios feature set. npm install node-fetch provides a spec-compliant fetch for older Node versions.

Use the https module when…

You need zero dependencies in a Node.js service and can handle the more verbose stream-based API. Good for serverless functions where bundle size matters.

2

Convert cURL GET to JavaScript fetch()

The simplest case — a GET request with headers. The URL is the first argument to fetch(), and headers go in the options object.

bashcURL GET with headers
curl 'https://api.example.com/users'   -H 'Accept: application/json'   -H 'Authorization: Bearer eyJhbGciOiJSUzI1NiJ9...'
javascriptJavaScript fetch() equivalent
const response = await fetch('https://api.example.com/users', {
  method: 'GET',    // optional for GET — it's the default
  headers: {
    'Accept': 'application/json',
    'Authorization': 'Bearer eyJhbGciOiJSUzI1NiJ9...',
  },
});

if (!response.ok) {
  throw new Error(`HTTP error: ${response.status}`);
}

const data = await response.json();
console.log(data);
javascriptAxios GET equivalent
import axios from 'axios';   // or: const axios = require('axios');

const { data } = await axios.get('https://api.example.com/users', {
  headers: {
    'Accept': 'application/json',
    'Authorization': 'Bearer eyJhbGciOiJSUzI1NiJ9...',
  },
});

console.log(data);  // already parsed JSON — no .json() call needed

Axios auto-parses JSON; fetch() does not

With fetch(), you must call await response.json() to parse the body. Axios parses JSON automatically — the response body is in response.data without any extra step. Axios also throws an error for non-2xx status codes; fetch() only rejects on network errors.
3

Convert cURL POST with JSON to JavaScript

POST requests with JSON bodies are the most common API call. The cURL -d flag becomes the body in fetch() or the second argument in Axios.

bashcURL POST with JSON body
curl -X POST 'https://api.example.com/users'   -H 'Content-Type: application/json'   -H 'Authorization: Bearer TOKEN'   -d '{"name": "Alice", "email": "alice@example.com", "role": "admin"}'
javascriptfetch() POST with JSON
const payload = {
  name: 'Alice',
  email: 'alice@example.com',
  role: 'admin',
};

const response = await fetch('https://api.example.com/users', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer TOKEN',
  },
  body: JSON.stringify(payload),   // fetch needs manual JSON.stringify
});

if (!response.ok) {
  const error = await response.text();
  throw new Error(`POST failed: ${response.status} — ${error}`);
}

const newUser = await response.json();
console.log('Created:', newUser.id);
javascriptAxios POST with JSON
const { data: newUser } = await axios.post(
  'https://api.example.com/users',
  {                            // Axios auto-serializes the object to JSON
    name: 'Alice',
    email: 'alice@example.com',
    role: 'admin',
  },
  {
    headers: { 'Authorization': 'Bearer TOKEN' },
    // Content-Type: application/json is set automatically
  }
);

console.log('Created:', newUser.id);

Common mistake: forgetting JSON.stringify with fetch()

Silent bug — sends [object Object]

❌ Bad
// Wrong: fetch does NOT auto-serialize objects
const response = await fetch(url, {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: { name: 'Alice' },  // ← sends "[object Object]" as the body!
});

Correct — serializes to valid JSON

✅ Good
// Correct: always JSON.stringify the body for fetch()
const response = await fetch(url, {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ name: 'Alice' }),  // ← correct JSON string
});
4

Convert cURL Authorization Headers to JavaScript

The -H 'Authorization: Bearer TOKEN' flag maps directly to the headers object. For Basic auth (-u user:pass), you need to base64-encode the credentials.

bashcURL auth patterns
# Bearer token
curl 'https://api.example.com/data' -H 'Authorization: Bearer TOKEN'

# Basic auth shorthand
curl 'https://api.example.com/data' -u 'username:password'

# API key in header
curl 'https://api.example.com/data' -H 'X-API-Key: my-secret-key'
javascriptJavaScript fetch() auth equivalents
// Bearer token
const response = await fetch(url, {
  headers: { 'Authorization': 'Bearer TOKEN' },
});

// Basic auth (curl -u username:password)
const credentials = btoa('username:password');  // base64 encode
const response = await fetch(url, {
  headers: { 'Authorization': `Basic ${credentials}` },
});

// API key in header
const response = await fetch(url, {
  headers: { 'X-API-Key': 'my-secret-key' },
});
javascriptAxios auth patterns
// Bearer token
const { data } = await axios.get(url, {
  headers: { 'Authorization': 'Bearer TOKEN' },
});

// Basic auth — Axios has a built-in auth option
const { data } = await axios.get(url, {
  auth: { username: 'username', password: 'password' },
  // Axios auto-generates the Authorization: Basic header
});

// API key in header
const { data } = await axios.get(url, {
  headers: { 'X-API-Key': 'my-secret-key' },
});
5

Convert cURL Form Data and File Uploads to JavaScript

When cURL uses -F (multipart form data) or --data-urlencode, the JavaScript equivalent uses the FormData API in browsers or the form-data package in Node.js.

bashcURL form data and file upload
# URL-encoded form data
curl -X POST 'https://api.example.com/login'   --data-urlencode 'username=alice'   --data-urlencode 'password=s3cr3t'

# Multipart file upload
curl -X POST 'https://api.example.com/upload'   -H 'Authorization: Bearer TOKEN'   -F 'file=@/path/to/report.pdf'   -F 'description=Q4 Report'
javascriptfetch() — form data in browser
// URL-encoded form data (browser)
const formData = new URLSearchParams();
formData.append('username', 'alice');
formData.append('password', 's3cr3t');

const response = await fetch('https://api.example.com/login', {
  method: 'POST',
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  body: formData.toString(),
});

// Multipart file upload (browser — uses a file input)
const fileInput = document.querySelector('#file-input');
const formData = new FormData();
formData.append('file', fileInput.files[0]);
formData.append('description', 'Q4 Report');

// Do NOT set Content-Type — browser sets it with the correct boundary
const response = await fetch('https://api.example.com/upload', {
  method: 'POST',
  headers: { 'Authorization': 'Bearer TOKEN' },
  body: formData,
});
javascriptAxios — file upload in Node.js
import axios from 'axios';
import FormData from 'form-data';  // npm install form-data
import fs from 'fs';

const form = new FormData();
form.append('file', fs.createReadStream('/path/to/report.pdf'));
form.append('description', 'Q4 Report');

const { data } = await axios.post('https://api.example.com/upload', form, {
  headers: {
    ...form.getHeaders(),           // includes Content-Type with boundary
    'Authorization': 'Bearer TOKEN',
  },
  onUploadProgress: (event) => {
    console.log(`Upload: ${Math.round(event.loaded / event.total * 100)}%`);
  },
});
6

Convert cURL PUT, PATCH, DELETE to JavaScript

bashcURL PUT, PATCH, DELETE
# PUT — full update
curl -X PUT 'https://api.example.com/users/42'   -H 'Content-Type: application/json'   -H 'Authorization: Bearer TOKEN'   -d '{"name": "Alice Smith", "email": "alice@new.com"}'

# PATCH — partial update
curl -X PATCH 'https://api.example.com/users/42'   -H 'Content-Type: application/json'   -H 'Authorization: Bearer TOKEN'   -d '{"email": "alice@new.com"}'

# DELETE
curl -X DELETE 'https://api.example.com/users/42'   -H 'Authorization: Bearer TOKEN'
javascriptfetch() PUT, PATCH, DELETE
const headers = {
  'Content-Type': 'application/json',
  'Authorization': 'Bearer TOKEN',
};
const url = 'https://api.example.com/users/42';

// PUT
const put = await fetch(url, {
  method: 'PUT',
  headers,
  body: JSON.stringify({ name: 'Alice Smith', email: 'alice@new.com' }),
});

// PATCH
const patch = await fetch(url, {
  method: 'PATCH',
  headers,
  body: JSON.stringify({ email: 'alice@new.com' }),
});

// DELETE
const del = await fetch(url, { method: 'DELETE', headers });
console.log(del.status);  // 204
javascriptAxios PUT, PATCH, DELETE
const config = { headers: { 'Authorization': 'Bearer TOKEN' } };
const url = 'https://api.example.com/users/42';

// PUT
await axios.put(url, { name: 'Alice Smith', email: 'alice@new.com' }, config);

// PATCH
await axios.patch(url, { email: 'alice@new.com' }, config);

// DELETE
await axios.delete(url, config);
7

Use the Online cURL to JavaScript Converter

For cURL commands from Chrome DevTools that have 15–20 browser-specific headers, the manual conversion is tedious. The online converter strips unnecessary browser headers and produces clean, working JavaScript code instantly.

1

Get the cURL command

From Chrome DevTools (F12 → Network → right-click request → Copy → Copy as cURL), from API docs, from Postman (Code → cURL), or write it manually.

2

Open the cURL converter

Go to unblockdevs.com/curl-converter. No login required, no data sent to any server — all conversion happens in your browser.

3

Select JavaScript / fetch or Axios

Choose your target: JavaScript fetch, Axios, Node.js https, or other supported languages. The converter generates clean code for the selected target.

4

Copy the code and use it

Click copy and paste into your project. The output handles headers, method, body, and auth — ready to run.

Supported output formats

The UnblockDevs cURL converter supports JavaScript (fetch), JavaScript (Axios), Python (requests), Python (httpx), Go, PHP, Ruby, Java, Swift, and more — all from a single paste of the original cURL command.

Frequently Asked Questions

Related API & HTTP Guides

Continue with closely related troubleshooting guides and developer workflows.