Skip to content

cURL to JavaScript: Convert Commands into Fetch, Axios, or Ky

JavaScript cannot execute a cURL command the way a shell does, but every part of a cURL request — the URL, the HTTP method, the headers, and the body — has a direct counterpart in JavaScript HTTP clients. This guide shows how to read a cURL command, translate it into fetch, Axios, or Ky, pull a ready-made cURL string out of browser DevTools, and decide when running the real cURL binary from Node.js (or using node-libcurl) is actually worth it.

Quick answer: the four parts that always map

Every cURL command you convert reduces to four pieces. Put the URL into the first argument of fetch (or the request call in Axios/Ky), set the method from -X/--request, copy each -H/--header into the headers object, and pass the -d/--data payload as the body. If the payload is JSON, serialize it with JSON.stringify in fetch; Axios and Ky accept a plain object and serialize it for you. The supplied cURL example and its fetch equivalent are shown below as reference modules — they are the exact originals, not regenerated code.

cURL flag to JavaScript field map

Use this as a translation checklist when you read a copied cURL command. The left column is the cURL flag as it appears in the command; the middle column is where it lands in fetch; the right column is the equivalent in Axios or Ky.

Fetch in the browser

In a browser, fetch is the built-in client. A GET request needs only the URL; a POST adds a method, headers, and a body. The supplied browser fetch example (code0003) shows both the promise-chain and async/await forms of the same GET call. For a POST with JSON, remember that fetch does not set Content-Type for you — you must include it in the headers object, and you must call JSON.stringify on the payload yourself.

Fetch in Node.js

Modern Node.js (18 and later) ships with fetch, so the same call works server-side. On older Node versions you install a polyfill such as node-fetch and import it explicitly. The supplied examples (code0004, code0005) show the install command and the ESM import pattern. The request flow is identical to the browser version; only the import line changes.

Axios as a structured replacement

Axios gives you a richer API than raw fetch: it serializes JSON bodies for you, exposes interceptors, and returns a response object with a data property. The supplied Axios example (code0006) shows a POST with a JSON body and a Content-Type header. If you are converting the supplied cURL POST, the Axios call takes the URL as the first argument, the payload object as the second, and the headers as the third.

Ky as a lightweight alternative

Ky is a small wrapper around fetch that keeps the minimal surface but adds a cleaner API and sensible defaults. The supplied Ky example (code0007) shows a POST with a json option, which Ky serializes for you. Choose Ky when you want something lighter than Axios but more ergonomic than raw fetch.

Extract a cURL command from the browser

All three major browsers let you copy a network request as a cURL command. This is the fastest way to see exactly what headers and payload the frontend sends. The steps differ slightly per browser.

Chrome, Safari, and Firefox steps

Chrome: open DevTools → Network, trigger the request, right-click the entry → Copy → Copy as cURL. Safari: enable the Develop menu first, then Develop → Show Web Inspector → Network, reload the page, right-click the request → Copy as cURL. Firefox: open DevTools → Network, run the request, right-click → Copy → Copy as cURL. Deeper walkthroughs for each browser are linked from the source registry.

Convert the copied cURL into JavaScript

Once you have the copied command, read off the method, headers, and body and place them into a fetch call. The supplied before/after pair (code0008 and code0009) shows the same POST expressed as cURL and as fetch. If you prefer not to do it by hand, an online cURL-to-JavaScript converter can generate the fetch or Axios code from the command string.

Running real cURL from Node.js with subprocesses

If you truly need the cURL binary — for example, to reuse a command that already works — Node.js can invoke it through child_process. The pattern is: Node calls the OS, the OS runs curl, and your Node code reads the output back. Requirements: curl installed on the machine, curl on PATH, and a Node.js environment (this never works in the browser). The supplied examples (code0010, code0011) show exec with promisify and parsing the stdout as JSON. This approach is handy for quick scripts and debugging, but for regular application code fetch or Axios is usually better: no dependency on curl being installed, more portable, and easier to test and handle errors.

Safety note on child_process

Treat child_process as a way to hand work to the operating system, not as a sandbox. Because these calls can run programs, touch files, and reach system utilities, the blast radius of a mistake is the whole machine, not just your app. Two habits keep that risk contained: never build a shell string from user input or other untrusted values, and prefer spawn with an argument array over exec with concatenated text so a value cannot be reinterpreted as a command. If a request can be expressed with fetch or Axios, that route avoids the shell entirely and is the safer default.

node-libcurl for cURL-style requests in Node.js

node-libcurl is a Node binding around the native libcurl library, so you get many of the same features and options that cURL exposes without spawning a subprocess. It is not a pure JavaScript library: it uses native bindings and talks to the underlying libcurl C library through compiled code. Requirements: Node.js (not the browser), a native toolchain (Python, make, C/C++ compiler), and on some platforms libcurl or SSL libraries present or bundled during installation. Install with the supplied command (code0012).

Simple GET with the curly API

The high-level curly API is the easiest entry point. In pure ESM you pull it in via createRequire. The supplied example (code0013) shows a GET with an Accept header and reads statusCode, headers, and data from the result. It is roughly equivalent to the supplied cURL one-liner (code0014), but you stay inside Node and get structured data back.

JSON POST with node-libcurl

The supplied POST example (code0015) sets postFields to a JSON.stringify'd payload and passes Content-Type and Accept headers explicitly, just like a cURL -X POST -H ... -d ... command. The difference is that you talk to libcurl through JavaScript instead of invoking the curl binary — useful when you want cURL-level power without spawning subprocesses.

Decision checklist: which client should you use?

Use fetch when you want zero dependencies and the request is simple; it is built into browsers and modern Node.js. Use Axios when you want interceptors, automatic JSON serialization, and a richer response object. Use Ky when you want a small wrapper over fetch with a cleaner API and good defaults. Use child_process with the real curl binary only for quick scripts or debugging where curl is guaranteed installed and the input is trusted. Use node-libcurl when you need cURL-level features inside Node without spawning subprocesses and can accept the native build requirements. Avoid Request — it is deprecated and only makes sense in old projects.

Failure distinctions worth knowing

A non-2xx HTTP status is not the same as a network failure. fetch resolves the promise for 4xx and 5xx responses — you must check res.ok or res.status yourself. Axios, by contrast, rejects the promise for non-2xx by default. A JSON.parse failure on subprocess output means the API did not return JSON (or returned an error page); check the raw stdout before parsing. A missing curl binary produces a spawn/exec error, not an HTTP error — catch it separately. And a Content-Type mismatch on a POST usually shows up as a 415 or an empty body, not as a network error.

cURL flag to JavaScript field map
cURL flagfetch optionAxios / Ky equivalent
URL (positional)first argument to fetchfirst argument to axios/ky call
-X / --requestmethodmethod (or axios.post / ky.post)
-H / --headerheaders objectheaders object
-d / --databody (JSON.stringify for JSON)data (Axios) or json (Ky)

Field names reflect the supplied examples; exact client behavior may vary by version.

Client choice at a glance
ClientWhere it runsWhen to pick it
fetchBrowser and Node 18+Zero dependencies, simple requests
AxiosBrowser and NodeInterceptors, automatic JSON, richer API
KyBrowser and Node (fetch-based)Lightweight wrapper with cleaner API
child_process + curlNode onlyQuick scripts, debugging, curl guaranteed installed
node-libcurlNode only (native build)cURL-level features without subprocesses
RequestLegacyDeprecated — old projects only

Runtime support reflects the supplied examples; verify against your installed versions.

Frequently asked questions

Can JavaScript run native cURL?

Not directly — JavaScript does not execute shell commands. It can fully replicate cURL behavior through HTTP clients: fetch in the browser, and fetch, Axios, or Ky in Node.js. The exception is Node.js, which can call the operating system through child_process.exec or spawn to run the real curl binary. That works but is uncommon in production because it depends on the OS, requires curl to be installed, and returns raw CLI output rather than structured data. node-libcurl is another Node-only option, but it needs native bindings and a more involved install.

How do I convert a cURL command to Fetch code?

Read the cURL command and map its pieces: the URL becomes the first argument to fetch, -X/--request becomes the method, each -H/--header becomes an entry in the headers object, and -d/--data becomes the body. Once you understand that mapping, converting is mostly copying values over. An online cURL-to-JavaScript converter can generate fetch or Axios code from the command when you do not want to do it by hand.

What is the best way to copy cURL from my browser?

Use DevTools: open the Network tab, trigger the request (reload the page or click the button that sends it), then right-click the request → Copy → Copy as cURL. That gives you a ready-made cURL command you can convert into JavaScript. It is the fastest way to see what the browser is actually sending.

Should I use Axios, Fetch, or Request?

Fetch is built into browsers and modern Node.js — a great default with no dependencies. Axios adds nicer defaults, interceptors, and a richer API. Request is deprecated and only makes sense in old projects. Ky is a lightweight wrapper around fetch with a cleaner interface. For new code, pick Fetch for simplicity, or Axios/Ky if you want a more ergonomic client.

curl -X POST https://example.com/api \
  -H "Content-Type: application/json" \
  -d '{"msg":"hello"}'
curl -X POST https://example.com/api \
  -H "Content-Type: application/json" \
  -d '{"msg":"hello"}'
curl -H "Accept: application/json" https://example.com/api