Skip to content

cURL to Code

Embed

Paste this where you want cURL to Code on your site. Adjust the height to fit.

<iframe src="https://www.browserutils.dev/embed/curl-to-code/" width="100%" height="500" frameborder="0" loading="lazy" title="cURL to Code"></iframe>
<p style="font-size:12px;margin:4px 0 0"><a href="https://www.browserutils.dev/tools/curl-to-code/">cURL to Code</a> by BrowserUtils</p>

Convert curl commands to code (JS fetch, Python requests, etc.).Runs in this tab.

How to use cURL to Code

  1. Paste a curl command into cURL Command. A line ending in a backslash is joined with the next line. DevTools "Copy as cURL" is the usual source.
  2. Pick a tab: JS Fetch, Python, PHP, or Node Axios.
  3. Copy the generated code. Unknown flags are ignored, and the first token that is not a flag is the URL.

A curl command in four languages

The page opens on this command:

curl -X POST https://api.example.com/users \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer token123" \
  -d '{"name": "John", "email": "john@example.com"}'

A trailing backslash joins the next line. JS Fetch writes:

const response = await fetch('https://api.example.com/users', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer token123',
  },
  body: JSON.stringify({"name": "John", "email": "john@example.com"}),
  redirect: 'manual',
});
const data = await response.json();
console.log(data);

redirect: 'manual' is there because the sample has no -L. Fetch follows redirects unless the option says otherwise, so a command without -L gets manual. Python writes:

import requests

headers = {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer token123',
}
json_data = {"name": "John", "email": "john@example.com"}

response = requests.post('https://api.example.com/users', headers=headers, json=json_data, allow_redirects=False)
print(response.json())

A DevTools GET

Copy as cURL for a GET with one auth header:

curl 'https://example.com/dashboard' -H 'Authorization: Bearer token123'

JS Fetch:

const response = await fetch('https://example.com/dashboard', {
  headers: {
    'Authorization': 'Bearer token123',
  },
  redirect: 'manual',
});
const data = await response.json();
console.log(data);

-b 'session=abc123' is copied as a Cookie header. -H 'Cookie: ...' does the same.

Flags

Flag What the generator does
-X, --request Sets the method
-H, --header Copies the header
-d, --data, --data-raw, --data-binary Sets the body, and switches GET to POST
-u, --user Basic auth. Fetch puts btoa(...) on the Authorization header. Python uses auth=(). PHP uses CURLOPT_USERPWD. Axios uses auth
-A, --user-agent Sets the User-Agent header
-b, --cookie Sets a Cookie header
-L, --location Follow redirects. Fetch omits redirect: 'manual'. Python omits allow_redirects=False. PHP sets CURLOPT_FOLLOWLOCATION. Axios omits maxRedirects: 0
-k, --insecure Python gets verify=False. PHP gets CURLOPT_SSL_VERIFYPEER set to false. Fetch has no way to skip TLS, so -k does not appear in that tab. Axios does not emit it either

curl -L -k https://example.com/old on the JS Fetch tab is a plain fetch('https://example.com/old') plus response.json(). The Python tab is requests.get('https://example.com/old', verify=False).

Unknown flags are ignored. The first token that is not a flag is the URL. -F, --data-urlencode, and cookie jars are not parsed.

The four tabs are JS Fetch, Python, PHP, and Node Axios. curlconverter.com and Trillworks cover more languages, including Go, Java, and C#.

Next step

Questions

How do I convert curl to fetch?
Paste the command and open the JS Fetch tab. The sample POST to https://api.example.com/users, with a JSON content type, a Bearer token, and a JSON body, becomes a fetch call with method POST, those two headers, JSON.stringify on the body, and redirect set to manual.
Is this curlconverter?
curlconverter.com and the older Trillworks converter cover more languages, including Go, Java, and C#. This page has four tabs (JS Fetch, Python, PHP, and Node Axios) and runs in the browser.
Which curl flags are converted?
-X and --request set the method. -H and --header copy headers. -d, --data, --data-raw, and --data-binary set the body and switch GET to POST. -u and --user set basic auth. -A and --user-agent set the User-Agent header. -b and --cookie set a Cookie header. -L and --location follow redirects. -k and --insecure is verify=False in Python and CURLOPT_SSL_VERIFYPEER false in PHP. JS Fetch has no TLS-skip option, so -k does not appear in that tab. -F, --data-urlencode, and cookie jars are not parsed.

More in Web & Networking

All 23 web & networking