Convert cURL to JavaScript
Generate browser-ready JavaScript code from cURL commands. Supports both modern Fetch API and legacy XMLHttpRequest for maximum compatibility.
Quick Example
Convert this cURL command:
curl -X GET https://api.example.com/users/123 \
-H "Authorization: Bearer YOUR_TOKEN"
Into JavaScript (Fetch):
fetch('https://api.example.com/users/123', {
method: 'GET',
headers: {
'Authorization': 'Bearer YOUR_TOKEN'
}
})
.then(response => response.json())
.then(data => console.log(data));
Fetch vs XMLHttpRequest
| Feature | Fetch API | XMLHttpRequest |
|---|---|---|
| Modern | ✅ Promise-based | ❌ Callback-based |
| Syntax | ✅ Clean & simple | ❌ Verbose |
| Browser Support | ⚠️ IE11 not supported | ✅ All browsers |
| Streaming | ✅ Built-in | ❌ Limited |
Common Patterns
POST with JSON Body
curl -X POST https://api.example.com/login \
-H "Content-Type: application/json" \
-d '{"username":"user","password":"pass"}'
Converts to:
fetch('https://api.example.com/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: 'user',
password: 'pass'
})
})
.then(response => response.json())
.then(data => console.log(data));
With Async/Await
async function login() {
try {
const response = await fetch('https://api.example.com/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: 'user',
password: 'pass'
})
});
const data = await response.json();
console.log(data);
} catch (error) {
console.error('Error:', error);
}
}
File Upload with FormData
curl -X POST https://api.example.com/upload \
-F "[email protected]" \
-F "title=My Photo"
Converts to:
const formData = new FormData();
formData.append('file', fileInput.files[0]);
formData.append('title', 'My Photo');
fetch('https://api.example.com/upload', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => console.log(data));
Browser Compatibility
- Fetch API: Chrome 42+, Firefox 39+, Safari 10.1+, Edge 14+
- XMLHttpRequest: All browsers including IE6+
Try It Live
Open the converter to transform your cURL commands, or use RequestBin to test the code with real endpoints.
Related docs: Python | Node.js | Headers & JSON