Curl to Fetch Converter

Convert curl commands into JavaScript fetch code for browser or Node.js API work.

Back to all tools on ToolForge

More in Web & Network



Fetch Code

About Curl to Fetch Converter

This curl-to-fetch converter turns curl commands into JavaScript `fetch` code for browser or Node.js use.

cURL to Fetch Conversion

// cURL command
curl "https://api.example.com/users" \
  -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer token" \
  -d '{"name":"John"}'

// Converted to fetch
fetch("https://api.example.com/users", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "Authorization": "Bearer token"
  },
  body: JSON.stringify({"name":"John"})
})
.then(res => res.json())
.then(data => console.log(data));

// Or with async/await
const response = await fetch(url, options);
const data = await response.json();

Fetch Options Reference

Option Description
method HTTP method (GET, POST, PUT, etc.)
headers Object of request headers
body Request body (POST/PUT/PATCH)
credentials Include cookies (omit/include/same-origin)

Frequently Asked Questions

What is the fetch API syntax?
The basic format is fetch(url, options). In options, you usually set method, headers, and body. For example, a JSON POST request sets method: "POST", a Content-Type header, and a string body.
How do I convert cURL to fetch?
Paste your cURL command and click Convert. The tool maps common flags like URL, -X method, -H headers, and -d body into a fetch call. Then you can copy and adjust the generated code in your app.
What is the difference between cURL and fetch?
cURL runs in a terminal, while fetch runs in JavaScript code (browser or Node.js). They do similar HTTP tasks, but the syntax and runtime context are different.
How do I handle async/await with fetch?
Use await fetch(...) inside an async function, then await response.json(). Wrap the request in try/catch so network and parsing errors are handled cleanly.