About the cURL to fetch converter
curl is the standard way to show an HTTP request in API docs and support tickets, and browsers can copy any network request as a curl command. Rewriting it as JavaScript by hand means carefully moving every header, the method and the body into a fetch() call.
This converter reads the curl command, including quotes, line breaks and the format from Chrome, Firefox and Edge's Copy as cURL, and writes the equivalent fetch() code with async/await or promises.
How to use the cURL to fetch converter
- Paste a curl command into the editor, or click Load sample. In browser DevTools, right-click a request in the Network tab and choose Copy > Copy as cURL (bash).
- Choose async/await or Promise .then() in the Style menu.
- Click Convert to fetch or press Ctrl + Enter (Cmd + Enter on Mac).
- Read any notes in the status bar, then copy the code into your project.
Examples
POST JSON with a bearer token
Input
curl -X POST https://api.example.com/v1/users \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d '{"name":"Ada","role":"admin"}'Output
const response = await fetch("https://api.example.com/v1/users", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer $TOKEN",
},
body: JSON.stringify({
name: "Ada",
role: "admin",
}),
});
if (!response.ok) throw new Error(`Request failed: ${response.status}`);
const data = await response.json();
console.log(data);Multipart form upload
Input
curl https://api.example.com/upload -F "file=@report.pdf" -F "title=Q3 report"Output
const formData = new FormData();
formData.append("file", fileInput.files[0]); // was @report.pdf
formData.append("title", "Q3 report");
const response = await fetch("https://api.example.com/upload", {
method: "POST",
body: formData,
});
if (!response.ok) throw new Error(`Request failed: ${response.status}`);
const data = await response.text();
console.log(data);In a browser, take the file from an <input type="file"> instead of a path on disk.
Common errors and how to fix them
Access to fetch has been blocked by CORS policy
curl ignores CORS but browsers don't. The API must send an Access-Control-Allow-Origin header for your site, or you call it from your own server instead of the browser.
The request works in curl but returns 415 Unsupported Media Type in fetch
The Content-Type header was dropped. When the body is JSON, set headers: { "Content-Type": "application/json" }; curl's -d sends form encoding unless you set it.
Paste a command that starts with curl.
The command was copied with a prompt in front, like $ curl or PS> curl. Remove everything before curl. In PowerShell, curl is an alias for Invoke-WebRequest, so copy the command from bash or Git Bash.
Good to know
- Supports
-X,-H,-d,--data-raw,--data-binary,--data-urlencode,--json,-F,-u,-b,-A,-e,-G,-Iand--max-time. A JSON body becomes a readableJSON.stringify({ … })object. - Like curl,
-dwithout-Xsends a POST withContent-Type: application/x-www-form-urlencoded, unless you set another content type. - Form uploads (
-F file=@photo.png) becomeFormData, with a placeholder where you need to pass a File or Blob. The copiedContent-Type: multipart/form-dataheader is dropped, because the browser has to add its own boundary. - Browsers don't let
fetchset headers likeCookie,User-AgentorReferer. They're kept in the code for Node.js, where they work, and the status bar explains it. - Commands copied in Windows cmd format, using
^for line breaks, aren't supported. Use Copy as cURL (bash) instead.
Frequently asked questions
Why does my request fail with a CORS error in the browser?
curl doesn't enforce CORS, but browsers do. If the API doesn't allow your site's origin, the same request that works in curl will be blocked in the browser. Make the call from your server or ask the API owner to allow your origin.
Does it work in Node.js?
Yes. Node.js 18 and newer include fetch, so the generated code runs there too, and headers such as Cookie and User-Agent are sent as written.
What should I do with API tokens in the command?
Treat them as secrets. Replace real tokens with a placeholder such as YOUR_TOKEN before sharing the code, load them from an environment variable in your app, and rotate a token if it was ever exposed.