cURL to Code Converter
Turn a curl command into fetch, axios or Python requests code.
About cURL to Code Converter
A cURL converter that turns a curl command, including Copy as cURL from browser dev tools, into JavaScript fetch, axios or Python requests code.
How to use
- Paste a curl command. In a browser's dev tools, right-click a request in the Network tab and choose Copy as cURL; the bash and the Windows cmd versions are both read.
- Choose the output: fetch, axios or Python requests. The code updates as you type.
- The shell is detected from the command, where
^"quoting or a^at a line end means Windows cmd. Set it by hand if the guess is wrong. - Read the list under the code: it names every option that was not carried over. Then copy the code or download it.
What it does
- Splits the command the way the shell would: single quotes, double quotes with backslash escapes,
#39;…'strings with\n,\xHHand\uHHHH, and backslash line continuations. For Windows cmd it applies the^escapes first and then the C runtime's backslash and quote rules. - Reads the URL and
-X,-H,-d,--data-raw,--data-binary,--data-urlencode,--json,-F,--form-string,-u,-b,-A,-e,--oauth2-bearer,-G,-I,-kand-L, including joined short flags such as-sSLand-XPOST. - Picks the method as curl does: POST when there is data or a form, HEAD for
-I, GET with the data moved into the query string for-G, and whatever-Xsays otherwise. A-dbody with no content type getsapplication/x-www-form-urlencoded, which is what curl sends. - A JSON body sent as JSON becomes an object literal, with
JSON.stringifyfor fetch andjson=for requests, but only when serialising it again gives back the same JSON. A body holding an integer above 2^53 or a repeated key stays a string. -ubecomes a BasicAuthorizationheader for fetch andauthfor axios and requests.-FbecomesFormDataor a requestsfileslist.-kbecomesverify=Falseor an https agent; fetch has no equivalent, so it gets a comment instead.- It does not expand shell variables such as
$TOKEN, and it does not read files named with@: uploads appear as placeholders in JavaScript and asopen()calls in Python. Proxies, timeouts, client certificates, output files and options it does not know are not converted, and each one is listed under the code. Only the first URL is used. - Without
-Lcurl does not follow redirects, while fetch, axios and requests follow them by default. The generated code leaves those defaults alone.
Example
curl https://api.example.com/users -u ada:secret -H 'Content-Type: application/json' -d '{"name":"Ada"}'
→
const response = await fetch('https://api.example.com/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic ' + btoa('ada:secret'),
},
body: JSON.stringify({
"name": "Ada"
}),
});
Privacy
The command is parsed in the browser. No request is made, to the URL in the command or to anywhere else, and nothing is stored. A command copied from dev tools usually carries cookies and tokens, so treat the generated code as a secret too.
Frequently asked questions
- How do I convert a curl command to fetch?
- The URL becomes the first argument to fetch, -X becomes the method, each -H becomes an entry in headers and -d becomes the body. curl sends POST whenever -d is present, so the method has to be set to POST even if the command has no -X.
- How do I copy a request as cURL from Chrome?
- Open DevTools, go to the Network tab, right-click the request and choose Copy, then Copy as cURL. On Windows there are two versions, cmd and bash; either can be pasted here. The copied command includes your cookies and authorisation headers.
- What is the Python requests equivalent of curl -d?
- requests.post(url, data=body) sends the same body. For a JSON body use json= with a dictionary, for -F use files=, and for -u use auth=(user, password).
- Why does my fetch call fail with a CORS error when the curl command works?
- curl is not a browser, so the same-origin policy does not apply to it. A page calling fetch needs the server to allow its origin with CORS headers, and the browser also ignores some headers set from script, such as Cookie and Origin. Running the code in Node or on a server avoids both.