cURL to JavaScript (fetch)

Convert a cURL command into browser-ready JavaScript using fetch.

cURL Command
JavaScript (fetch) Output
JavaScript (fetch) code appears here

Related Tools

Documentation

What is curl-to-JavaScript?

This tool converts a curl command into a browser-ready JavaScript snippet using the standard fetch API — the kind of code you'd paste into a <script> tag, a frontend module, or directly into DevTools. It's a separate target from cURL to Node.js because the two environments don't share every global: a browser has no Buffer and no filesystem, so the generated code for Basic Auth and file uploads takes a different path in each.

How it works

The same curl parser used across every cURL converter on this site extracts method, URL, headers, body, form fields, and auth from the command. Method, headers, and body map directly onto fetch's options object, with the body passed through as a raw string rather than re-parsed and re-serialized as JSON — so the exact bytes curl would have sent are what fetch sends too. The one browser-specific substitution is -u user:pass: instead of Node's Buffer.from(...).toString('base64'), the generator emits btoa('user:pass'), the browser-native base64 encoder, and builds the Authorization header from that at runtime rather than hardcoding a precomputed value.

Features

  • Uses the standard, cross-browser fetch API — no framework, no bundler required
  • Body passed through as a raw string, not re-encoded
  • -u Basic Auth computed via btoa() at runtime
  • -F/--form uploads become a FormData instance, with file fields stubbed for a real File object
  • Uses top-level await, ready to paste into a <script type="module"> or the console

Example

Input: curl -X POST https://api.example.com/v1/login -H "Content-Type: application/json" -u admin:s3cret -d '{"username":"admin","remember":true}'

Output:

const credentials = btoa('admin:s3cret');

const response = await fetch('https://api.example.com/v1/login', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': `Basic ${credentials}`,
  },
  body: '{"username":"admin","remember":true}',
});

const data = await response.text();
console.log(response.status, data);

Common errors

A request to a different origin than the page running this script will hit the browser's CORS policy — if the target server doesn't send an Access-Control-Allow-Origin header permitting your origin, the browser blocks reading the response even though the request itself went through. That's not something this generator can fix, since CORS is enforced by the server's response headers, not the request. Top-level await also requires a module context — plain inline <script> tags need type="module", or you'll need to wrap the code in an async function.

Best practices

Check response.ok before trusting the body — fetch only rejects on network failure, never on a 4xx/5xx status. Never hardcode real credentials into client-side JavaScript that ships to users' browsers; the -u flow shown here is meant for testing against your own APIs, not for shipping a secret to production frontend code where anyone can read it from DevTools.

Frequently Asked Questions

How is this different from the Node.js target?

The generated call is nearly identical — both use fetch with the same options object — but this one is meant to run in a browser tab, not a Node process. It uses btoa() for Basic Auth instead of Buffer (which doesn't exist in a browser), and the file-upload TODO points at a element instead of the filesystem.

Will this work if I paste it straight into the browser console?

Yes, for same-origin or CORS-enabled requests. If the target API doesn't send CORS headers permitting your origin, the browser will block the response even though the request the API sees is identical — that's a server-side CORS policy, not something this generator can work around.

How is Basic Auth handled without Buffer?

Via the browser's built-in btoa(), which base64-encodes a string the same way Buffer.from(...).toString('base64') does in Node. btoa() only handles Latin1 text, so a username or password with non-ASCII characters needs an extra encoding step this generator doesn't add automatically — a rare case, but worth knowing if login fails with valid-looking credentials.

Does it support file uploads via -F?

Non-file fields are appended to a FormData instance directly. File fields are stubbed with a TODO, since there's no filesystem to read from in a browser — the real value has to come from a file input or drag-and-drop event.