cURL to JavaScript XHR Converter
Convert curl commands to JavaScript code using XHR. Paste a command and the XHR snippet appears instantly — everything runs in your browser.
- Instant ConversionCode updates as you type
- Multiple Languages40 languages, 61 libraries
- 100% Client SideYour data stays private
How to use this tool
Steps for using the cURL to Code.
- Copy a curl command — from API documentation, or from your browser's DevTools (Network tab → right-click a request → Copy as cURL (bash)).
- Paste it into Enter cURL command. The JavaScript XHR code appears instantly; other JavaScript libraries (fetch, Axios, jQuery) are in the tabs above the code.
- Set up: Nothing to install.
- Run it: Run it in the browser console or your page script. Replace placeholders such as
YOUR_TOKENfirst, and check any notes under the editors for curl options that have no XHR equivalent.
Example
GET with headers → JavaScript XHR
curl 'https://api.example.com/v1/users?page=2' \
-H 'Accept: application/json' \
-H 'Authorization: Bearer YOUR_TOKEN'const xhr = new XMLHttpRequest();
xhr.open('GET', 'https://api.example.com/v1/users?page=2');
xhr.setRequestHeader('Accept', 'application/json');
xhr.setRequestHeader('Authorization', 'Bearer YOUR_TOKEN');
xhr.responseType = 'json';
xhr.onload = () => {
console.log(xhr.status, xhr.response);
};
xhr.onerror = () => {
console.error('Request failed');
};
xhr.send();Common use cases
- Very old browsers
- Upload progress events
- Legacy code
Converting curl to JavaScript XHR
XMLHttpRequest is the original browser API for HTTP requests, supported everywhere.
The converter reads the curl command the way curl does — the method (-X), headers (-H), request bodies (-d, --data-raw, --json, -F), query strings, basic auth (-u) and cookies — and writes the equivalent XHR code. Options that have no XHR equivalent are listed under the editors rather than silently dropped.
Other libraries for JavaScript, each with its own page: fetch, Axios, jQuery.
Limitations
- In a browser, the API must allow your site through CORS; requests that work with curl can be blocked by the browser otherwise.
- The code is a starting point: add timeouts, retries and error handling for production use.
- Secrets in the curl command are copied into the code — move them to environment variables before committing.
Frequently asked questions
Why use XHR instead of fetch?
Mainly for legacy code, or upload progress events (xhr.upload.onprogress), which fetch doesn't offer directly.
How do I read JSON with XHR?
Parse xhr.responseText with JSON.parse, or set xhr.responseType = 'json'.
Is my curl command sent to a server?
No. The conversion runs entirely in your browser, so tokens and passwords in the command never leave your device. Still, replace real secrets before sharing the generated code.