cURL to fetch / Axios Guide
What is a cURL to fetch or Axios converter?
A curl to fetch converter takes a shell command and rewrites its request details into JavaScript. Instead of reading -X POST, -H "Authorization: Bearer ...", and --data '{...}' and translating each part by hand, you get code that is already shaped like fetch() or Axios configuration.
That is useful because API docs still lean heavily on curl. The docs may show a terminal command even when the real implementation target is a browser app, a React component, a Node script, or a test harness. A curl to axios or curl command to fetch tool closes that gap quickly.
The catch is runtime context. curl is a CLI HTTP client. fetch and Axios in the browser are constrained by the web platform. Toolzy.dev keeps the conversion local in the browser for privacy, but the result still needs engineering judgment because the browser cannot reproduce every feature that curl can express. Shell prompts and line continuations are normalized, while quoted multiline request bodies stay together with line breaks preserved in the generated request string. File-backed payload forms like -d @file, --data-binary @file, -F file=@avatar.png, and -T payload.bin still need manual rewriting because the browser cannot read local disk files for the conversion.
How to use this tool
- Paste the full
curlcommand, including headers and body flags. - Choose the output target:
fetchor Axios. - Generate the JavaScript.
- Review the method, headers, body encoding, and auth handling.
- Copy the result and adapt it to your app's runtime, tokens, and error handling.
For the cleanest output, start with a minimal request. Commands copied from browser DevTools often include extra headers that are technically valid in transit but noisy or unusable in handwritten code.
Common use cases
- Converting API documentation examples into working frontend requests
- Turning a terminal reproduction command into code for a React or Astro component
- Translating Postman-exported or DevTools-copied requests into readable JavaScript
- Building test helpers from one-off
curlcalls used during debugging - Comparing whether an integration is simpler in native
fetch()or in Axios - Bootstrapping a request before moving auth, retries, and error handling into shared helpers
How curl maps to JavaScript requests
A basic command like this:
curl -X POST "https://api.example.com/users" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer TOKEN" \
--data '{"name":"Ada"}'
usually maps to fetch() like this:
await fetch("https://api.example.com/users", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer TOKEN",
},
body: JSON.stringify({ name: "Ada" }),
});
or to Axios like this:
await axios({
url: "https://api.example.com/users",
method: "post",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer TOKEN",
},
data: { name: "Ada" },
});
That mapping is straightforward for URL, method, headers, and body. It gets less straightforward once the original command depends on terminal-specific behavior.
Browser limitations that curl does not have
This is the part that matters most when you convert curl to javascript.
CORS
curl can call any reachable URL. Browser JavaScript cannot. If the target API does not send the correct Access-Control-Allow-Origin headers, the browser blocks the response even when the request itself reached the server.
That means a converted request can be semantically correct and still fail at runtime. The fix is usually server configuration, a same-origin proxy, or moving the request to backend code.
Forbidden and browser-controlled headers
Some headers should be removed from generated code because browsers control them or forbid setting them directly. Common examples include Host, Content-Length, Origin, Referer, User-Agent, and many sec- headers copied from DevTools.
If a generated curl to fetch result includes these, do not assume they belong in the final code. They often create confusion without helping the request succeed.
Cookies and credentials
curl can load cookies from flags, jars, or direct header values. Browser requests depend on the current origin, cookie policy, SameSite, and whether you explicitly send credentials. Even with credentials: "include", the server must opt in correctly for cross-origin cookie use.
TLS, proxies, and networking
Flags like --proxy, --cert, --key, --cacert, --resolve, or -k belong to the network stack beneath the browser page. They do not translate into normal frontend JavaScript. If the original request needs them, you likely need a local proxy, backend relay, or different execution environment.
Unsupported flags and manual cleanup cases
Some curl commands are really small scripts disguised as one-liners. Converters can parse them, but they cannot always preserve intent exactly.
Manual cleanup is common when the command includes:
- multipart uploads with several
--formparts - file-backed payload forms like
-d @file,--data-binary @file,-F file=@avatar.png, and-T payload.bin - cookie jar round-tripping across requests
- retry logic or timeout tuning
- compressed transfer flags
- redirect policies that differ from your target runtime
- shell interpolation like
Authorization: Bearer $TOKEN - command substitution or environment-variable expansion
In those cases, the generated output is still valuable because it extracts the request shape. You just need to rebuild the missing runtime behavior yourself.
fetch vs Axios: when to pick each output
Use fetch when you want the browser-native primitive and minimal dependencies. It is a good fit for lightweight tools, simple UI actions, and codebases that already normalize responses in utility functions.
Use Axios when your project already relies on interceptors, shared instances, request cancellation conventions, or consistent JSON/error handling. If your app has apiClient.interceptors.response.use(...), converting straight to Axios often produces less follow-up editing.
Neither choice fixes browser restrictions. Axios still uses the browser transport underneath, so CORS, credentials, and header limitations apply there too.
Safer review checklist after conversion
Before pasting generated code into production code, review these items:
- confirm the URL does not include temporary environment hosts
- remove copied debugging headers and browser-generated headers
- replace hard-coded auth strings with your app's token flow
- check whether the body should stay raw text,
FormData, orJSON.stringify(...) - decide whether the request belongs in the browser or should move server-side
- add explicit error handling instead of trusting a demo snippet
This review step matters more than the conversion itself. The goal is fast translation, not blind execution.
Troubleshooting
Why does the converted code fail while the original curl works? — The most common reason is browser policy: CORS, credentials, mixed content, or forbidden headers. curl is not subject to the same restrictions.
Why did the tool not preserve every flag? — Some curl flags represent terminal transport features that have no direct equivalent in browser JavaScript. Proxy, TLS, cookie jar, and retry flags usually require manual implementation.
Why did my quoted multiline body survive? — The normalizer keeps request bodies inside quotes together and preserves line breaks in the generated request string, often through \n escapes, while flattening unquoted line breaks and shell prompt markers around the command itself.
Why does the generated code include odd headers from DevTools? — Requests copied from DevTools often include transient browser headers. Remove sec- headers, Host, Content-Length, and other browser-controlled values before using the code as a maintained source file.
Can this convert curl into code that bypasses CORS? — No. No frontend converter can remove browser security policy. If the target origin does not allow the request, you need server changes or a backend proxy.
Will cookies from my terminal session carry over? — No. Terminal cookies, cookie jars, and shell environment state do not automatically exist in the browser. You need browser-side credentials support and compatible server cookie settings.
Is my request private when I paste it here? — Yes. Toolzy.dev performs the conversion in the browser, so the command stays local to your machine.