Convert cURL to fetch or axios: How -d, -H and -u Map to JavaScript
Last updated: · 9 min read
API documentation usually shows its example requests as curl commands, and browser developer tools let you copy any request as curl. To send the same request from JavaScript, the method, headers, body and credentials each have to move into a fetch or axios option, and the mistakes hide in the details: -d silently switches the method to POST, a body without a Content-Type is treated as form data, and the password given with -u ends up Base64-encoded in a header. This guide explains what the cURL to JavaScript Converter does with each option, what it leaves alone, and why a request that works in curl can behave differently in a browser. The outputs in the examples are the tool's real results.
Formula
Rules:
1. The command is split into words the way a shell does it: text in single quotes is taken literally, the escapes \" \\ \$ \` are resolved inside double quotes, and $'…' strings and a trailing \ (or ^ on Windows) for line continuation are supported.
2. Method: the value of -X if present; otherwise HEAD for -I, POST when there is data (-d, --json) or -F, and GET in every other case.
3. The pieces given with -d, --data-raw, --data-binary and --data-urlencode are joined with &; if no Content-Type is set, application/x-www-form-urlencoded is added. --json also adds Content-Type and Accept: application/json.
4. A body that is a valid JSON object or array, sent with a Content-Type containing json, is written as JSON.stringify({…}) for fetch and as a plain object for axios; any other body stays a string.
5. With -G the data goes into the query string of the URL instead of the body.
6. -u user:password becomes an Authorization: Basic … header for fetch and an auth object for axios; -b becomes a Cookie header, -A a User-Agent header and -e a Referer header.
7. -F fields are appended to a FormData object; -L is written as redirect: "follow" for fetch.
8. Options the tool does not know are not converted; they are listed in a warning line at the top of the output.
Input 1 (target: fetch):
curl -X POST https://api.example.com/users \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_TOKEN" \
-d '{"name": "Ada Lovelace", "role": "admin"}'
Output 1:
const response = await fetch("https://api.example.com/users", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_TOKEN"
},
body: JSON.stringify({
"name": "Ada Lovelace",
"role": "admin"
})
});
const data = await response.json();
console.log(data);
// Supported curl options: -X, -H, -d, --data-raw, --data-binary, --data-urlencode, --json, -F, --form-string, -u, -b, -A, -e, -G, -I, -L, -k, --url. Options that do not change the request (-o, -s, -v, -i, --compressed…) are skipped; any others are listed in a warning.
Input 2 (target: fetch):
curl -G "https://api.example.com/search" --data-urlencode "q=café latte" -d "page=2" -u "demo:s3cret" -L -k --max-time 10
Output 2 (the closing note is the same as in Output 1 and is not repeated here):
// Warning: unsupported curl options were ignored: --max-time
// Warning: -k/--insecure has no browser equivalent; certificate verification cannot be turned off from a script.
const response = await fetch("https://api.example.com/search?q=caf%C3%A9+latte&page=2", {
headers: {
"Authorization": "Basic ZGVtbzpzM2NyZXQ="
},
redirect: "follow"
});
const data = await response.text();
console.log(data);
Input 2, target: axios:
// Warning: unsupported curl options were ignored: --max-time
// Warning: -k/--insecure has no browser equivalent; certificate verification cannot be turned off from a script.
import axios from "axios";
const response = await axios({
method: "get",
url: "https://api.example.com/search?q=caf%C3%A9+latte&page=2",
auth: {
username: "demo",
password: "s3cret"
}
});
console.log(response.data);There is no formula here, only a set of mapping rules. They follow curl's own manual: -d sends the data with POST, the way a browser submits a form, using the content type application/x-www-form-urlencoded, and --json is a shortcut for --data-binary plus the Content-Type and Accept headers. The tool writes code and never sends the request, so it cannot tell you how the server will answer or whether the code runs. The generated code uses top-level await: run it in an ES module or inside an async function.
What the inputs mean
- cURL command
- A single command that starts with curl. Commands broken over several lines with a trailing \ are accepted, as are a leading $ prompt and the spelling curl.exe. Up to 1,000,000 characters are processed.
- Target
- fetch (the default) or axios. The fetch output needs no extra package; the axios output starts with import axios from "axios", carries credentials in an auth object and puts the body in the data field.
Worked example: Converting a JSON POST and an authenticated search
- Input 1 spans four lines; the backslashes at the line ends join them into one command. -X POST sets the method: method: "POST".
- The two -H options go into the headers object in the order they were written: "Content-Type": "application/json" and "Authorization": "Bearer YOUR_TOKEN".
- The body given with -d is a valid JSON object and the Content-Type contains json, so it is written as body: JSON.stringify({ … }) rather than as a string, and the response is read with const data = await response.json();.
- Input 2 uses -G: the value of --data-urlencode "q=café latte" is encoded as q=caf%C3%A9+latte (the space becomes +, the letter é becomes two UTF-8 bytes), joined to -d "page=2" with & and appended to the URL. The method is GET, so no method line is written.
- -u "demo:s3cret" turns into the header "Authorization": "Basic ZGVtbzpzM2NyZXQ=" in the fetch output. That value is the Base64 form of demo:s3cret; it is not encryption, and anyone who sees the header can decode it.
- -L produces the line redirect: "follow". --max-time is not among the converted options and -k has no counterpart in a browser, so both are reported at the top of the output: Warning: unsupported curl options were ignored: --max-time and Warning: -k/--insecure has no browser equivalent; certificate verification cannot be turned off from a script.
- Converted with the axios target, the same command puts the credentials into auth: { username: "demo", password: "s3cret" } instead of a header; axios does the Base64 encoding itself.
Result: Input 1 becomes a fetch call with method, headers and a JSON.stringify body. Input 2 becomes a GET request to https://api.example.com/search?q=caf%C3%A9+latte&page=2 that carries a Basic credentials header and starts with two warning lines.
🔁 cURL to JavaScript Converter (fetch/axios)
Free, no sign-up; your data stays in your browser.
Open the calculator →How to read the result
Treat the output as a correct starting point, not as finished code. curl and a browser send the same request under different conditions. First, CORS: curl is not subject to origin checks, whereas fetch in a browser looks for the server's Access-Control-Allow-Origin response on cross-origin requests and sends a preflight request before anything that carries an Authorization header or an application/json body. Second, forbidden headers: the Fetch Standard does not let scripts set headers such as Cookie and Referer; the tool writes -b and -e as headers, but a browser ignores them, and cookies are controlled through the credentials option instead. Third, redirects: curl follows them only with -L, while fetch follows them by default; write redirect: "manual" if you want curl's behaviour without -L. JSON.stringify also rewrites the body without whitespace, so for requests signed over the body (HMAC) the bytes differ from those in the original command. On security: the conversion runs in your browser and the command is not sent anywhere. Even so, replace real access tokens, session cookies and passwords with placeholders such as YOUR_TOKEN before pasting a command into any website, and do not hard-code them in front-end code. This is general information.
Common mistakes
- Pasting a command copied from developer tools as it is. Those commands usually contain your session cookie and access token; swap them for placeholders first.
- Leaving a URL that contains & unquoted. In curl https://api.example.com/items?page=1&limit=5 the shell treats everything after & as a separate command; the tool converts only the page=1 part and reports: Warning: the command was cut at the unquoted &; the shell treats what follows as a separate command. If the character belongs to the URL, wrap the URL in quotes.
- Expecting $TOKEN or $(…) to be resolved. The tool does not expand shell variables; -H "Authorization: Bearer $TOKEN" reaches the output as "Bearer $TOKEN", and you have to supply the value in code yourself.
- Assuming a body read from a file with -d @payload.json will appear in the code. The tool cannot access your files; it writes a request without a body and warns: Warning: -d reads from a file (payload.json); its contents cannot be included here, add them yourself.
- Combining -X GET with -d. fetch does not accept a body on GET or HEAD requests, and the tool says so: Warning: fetch does not send a body with GET or HEAD requests; change the method or move the data into the URL. Use -G to move the data into the URL.
- Sending JSON with -d but without a Content-Type. curl posts that body as application/x-www-form-urlencoded; the tool does the same and keeps the body as a string. Add -H "Content-Type: application/json" or use --json.
- Passing several URLs in one command. Only the first is converted: Warning: the command contains more than one URL; only the first was converted.
Frequently asked questions
What is the difference between curl -d and --data-raw?
Both send the data as the POST body. The difference is the @ character: -d treats a value starting with @ as a file name and reads that file (stripping newlines on the way), while --data-raw gives @ no special meaning. --data-binary sends a file without touching its newlines. For -d @file and --data-binary @file the tool reports that it cannot read the file; a --data-raw value is written into the body as it is.
Why does a request that works in curl fail with a CORS error in the browser?
curl does not enforce the same-origin policy; browsers do. If your page and the API are on different origins, the server has to allow your origin with the Access-Control-Allow-Origin header and answer the preflight request for custom headers. That cannot be fixed in client code: it is a server setting, or the request has to go through your own back end.
Should I use fetch or axios?
fetch is built into browsers and needs no package, but it does not throw on a 404 or 500 response; you check response.ok yourself. axios is a separate package; with its default settings it throws on status codes outside 2xx, parses JSON bodies for you and takes Basic credentials through the auth option. The tool generates either.
What is the JavaScript equivalent of -k (--insecure)?
In a browser there is none: certificate verification cannot be switched off from a script, and the tool writes a warning to that effect. The lasting fix is a valid certificate on the server; -k is for local experiments only.
How is a file upload with -F converted?
The fields are appended to a FormData object. Text fields are written with their value; for a file field such as -F "file=@report.pdf" the tool produces form.append("file", fileInput.files[0], "report.pdf"); and asks you to replace fileInput.files[0] with your own File or Blob. Do not set the Content-Type header by hand when sending FormData; the browser adds the boundary.
Is my command, with the keys in it, sent to a server?
No, the conversion happens in your browser. The safest habit is still never to paste real keys, cookies or passwords into an online tool; replace them with placeholders first.
The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.