Convert curl to JavaScript
curl2code converts curl commands to JavaScript code using the native fetch API. Everything runs locally in your browser via WebAssembly for complete privacy. If you need server-side Node.js code, try our curl to Node.js converter. For type safety, check the curl to TypeScript converter. Below are practical examples for common HTTP patterns.
How to copy curl from your browser
- 1
Open DevTools
Press F12 or Ctrl+Shift+I to open your browser developer tools.
- 2
Go to the Network tab
Click the Network tab and perform the action that triggers the HTTP request.
- 3
Copy as cURL
Right-click the request → Copy → Copy as cURL. Then paste it above.
Frequently Asked Questions
What is the Fetch API?
The Fetch API is a modern, promise-based interface built into all browsers for making HTTP requests. It replaces the older XMLHttpRequest with a cleaner, more powerful API. Fetch supports streaming, request/response objects, CORS, and integrates naturally with async/await. No installation needed — it's available globally. curl2code uses Fetch as the default output for JavaScript conversions.
Fetch vs XMLHttpRequest vs jQuery.ajax — which should I use?
Fetch is the modern standard — use it for new projects. XMLHttpRequest is legacy but still works everywhere and supports progress events natively. jQuery.ajax adds convenience if jQuery is already in your project. For server-side JavaScript, see our curl to Node.js converter. For type safety, try curl to TypeScript.
How to handle authentication with Fetch?
Pass an Authorization header in the headers option: fetch(url, { headers: { "Authorization": "Bearer token" } }). For Basic auth, encode credentials with btoa(user + ':' + pass). curl2code detects -u and -H 'Authorization: ...' flags automatically.
How to send multipart form data with Fetch?
Create a FormData object and pass it as the body: fetch(url, { method: "POST", body: formData }). Do not set Content-Type manually — the browser adds the multipart boundary automatically. curl2code converts -F flags to FormData.
How to handle errors with Fetch?
Fetch only rejects on network failures, not HTTP errors. Always check response.ok or response.status before reading the body. Wrap calls in try/catch with async/await to handle both network and parsing errors in one place.
How to use async/await with Fetch?
Fetch returns a Promise, so use const response = await fetch(url) followed by const data = await response.json(). Wrap in a try/catch block for error handling. This is cleaner than .then() chains and is the recommended pattern for modern JavaScript.
How to set a timeout for Fetch requests?
Use AbortController: create a controller, pass { signal: controller.signal } to fetch, and call controller.abort() after a delay with setTimeout. Modern browsers also support AbortSignal.timeout(5000) as a simpler one-liner. curl's --max-time maps to this pattern.
How to handle CORS issues with Fetch?
CORS is enforced by the browser, not by Fetch itself. Set mode: 'cors' (the default) and ensure the server sends proper Access-Control-Allow-Origin headers. For credentials (cookies), add credentials: 'include'. CORS does not apply to server-side code — for that, see our curl to Node.js converter.
How to send a POST request with JSON body in JavaScript?
Convert a curl POST command like curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' URL to JavaScript using curl2code. The generated code uses fetch(url, { method: 'POST', body: JSON.stringify(data), headers: {'Content-Type': 'application/json'} }). curl2code automatically detects -d/--data flags with JSON content and sets the appropriate Content-Type header in the fetch code.
How to add Bearer token authorization in JavaScript?
Pass curl -H "Authorization: Bearer YOUR_TOKEN" URL to curl2code and it generates fetch code with the auth header: headers: { 'Authorization': 'Bearer YOUR_TOKEN' }. curl2code detects Bearer tokens from both -H "Authorization: Bearer ..." and --oauth2-bearer flags. For Basic auth, use -u user:pass.
How to set Content-Type header in JavaScript?
curl2code converts curl -H "Content-Type: application/json" URL to fetch code with the proper header: headers: { 'Content-Type': 'application/json' }. For -d data, curl defaults to application/x-www-form-urlencoded; for -F form uploads, it uses multipart/form-data. curl2code maps each to the correct fetch API.