cURL to Code Converter

Paste a cURL command, for example from your browser's "Copy as cURL" or from API docs, and get the same request in Python, JavaScript, Node.js, Go, Java, PHP, C# or Ruby.

cURL command

Examples:

Code


      

What this tool does

Paste a cURL command and get the same HTTP request as code in eight languages: Python (requests), JavaScript (fetch), Node.js (axios), Go (net/http), Java (HttpClient), PHP (the cURL extension), C# (HttpClient) and Ruby (Net::HTTP). The code updates as you type, and a row of chips above it summarises what was understood: method, host, number of headers, the kind of body, authentication and options such as a timeout.

The converter reads the command the way a shell would (bash quoting, Windows cmd's ^ escapes, line continuations), keeps curl defaults that are easy to miss, such as the form Content-Type that -d sends, and escapes every string by the rules of the target language. Anything that couldn't be converted is listed under the code rather than silently dropped. The conversion runs in this page, so tokens and cookies in the command stay on your machine.

How to use it

  1. Paste a command into the cURL command box. Or click one of the examples: JSON POST, GET with token, Form + basic auth, File upload or Chrome "Copy as cURL".
  2. Pick a language with the buttons above the code. Python is the default; your choice is remembered in this browser.
  3. Decide on browser headers. Commands copied from a browser carry headers like sec-ch-ua, sec-fetch-mode and priority that servers rarely need. Tick Leave out browser-only headers to drop them.
  4. Read the summary and the notes. Check the chips (for example POST · api.example.com · 2 headers · JSON body · bearer token) and the notes below the code.
  5. Press Copy and paste the code into your project. Replace hard-coded tokens with an environment variable or secret store before you commit it.

Examples

JSON POST to Python (requests)

cURL

curl -X POST https://api.example.com/v1/users \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer YOUR_TOKEN' \
  -d '{"name": "Ada Lovelace", "roles": ["admin"], "active": true}'

Python

import requests

headers = {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer YOUR_TOKEN',
}

json_data = {
    'name': 'Ada Lovelace',
    'roles': [
        'admin',
    ],
    'active': True,
}

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

print(response.status_code)
print(response.text)

The JSON body becomes a Python dict passed as json=, with true written as True. In JavaScript (fetch) the same body becomes JSON.stringify({ … }); in Go, Java, PHP, C# and Ruby the original JSON text is sent as a string.

Form data and Basic auth to Go

cURL

curl -u 'client_id:client_secret' -d 'grant_type=client_credentials' -d 'scope=read write' https://auth.example.com/oauth/token

Go (the part inside main)

data := strings.NewReader("grant_type=client_credentials&scope=read write")

req, err := http.NewRequest("POST", "https://auth.example.com/oauth/token", data)
if err != nil {
	log.Fatal(err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.SetBasicAuth("client_id", "client_secret")

The command has no -X and no Content-Type, but curl sends -d data as a POST with application/x-www-form-urlencoded, so the code does the same. The two -d values are joined with &. The full output also includes the package, the imports and the code that reads the response.

File upload to Node.js (axios)

cURL

curl -L -F 'title=Holiday photo' -F 'photo=@./photo.jpg;type=image/jpeg' https://api.example.com/v1/uploads

Node.js

import axios from 'axios';
import { openAsBlob } from 'node:fs';

const form = new FormData();
form.append('title', 'Holiday photo');
form.append('photo', await openAsBlob('./photo.jpg', { type: 'image/jpeg' }), 'photo.jpg');

const response = await axios({
  method: 'post',
  url: 'https://api.example.com/v1/uploads',
  data: form,
});

console.log(response.status);
console.log(response.data);

-F makes a multipart form. The file's content type from ;type= is kept, and the multipart Content-Type header is left to the library, because it has to contain a boundary that only the library knows. axios follows redirects by default, so -L needs no extra code here.

A Windows cmd "Copy as cURL" to Python

cURL (cmd syntax)

curl ^"https://api.example.com/v1/items?id=7^&sort=asc^" ^
  -H ^"accept: application/json^" ^
  --data-raw ^"^{^\^"name^\^":^\^"Ada^\^"^}^"

Python (excerpt)

headers = {
    'accept': 'application/json',
    'Content-Type': 'application/json',
}

json_data = {
    'name': 'Ada',
}

response = requests.post(
    'https://api.example.com/v1/items?id=7&sort=asc',
    headers=headers,
    json=json_data,
)

In cmd, ^ escapes the next character and \" is a literal quote, so ^\^" is one " in the body. The tool also adds a note: "No Content-Type was given. curl would send this as form data, but it looks like JSON, so JSON was chosen." Check that this is what the server expects.

Common uses

  • Turning API documentation into codeMost API references give a cURL example for every endpoint. Paste it and get a starting point in the language you're working in.
  • Automating something you do in a web appCopy the request from the browser's Network tab, convert it, and you have a script that does the same thing, for example exporting a report.
  • Reproducing a bug reportA cURL command in a ticket is precise but awkward to run in a test. Convert it to the language of your test suite.
  • Learning an HTTP librarySwitch languages to see where each library puts headers, auth and multipart fields.

How to get a cURL command

In Chrome, Edge or Firefox, open the developer tools (F12), go to the Network tab, right-click a request and choose Copy → Copy as cURL. On Windows the browsers offer a cmd and a bash (or POSIX) version; both work here. Postman, Insomnia and most API docs can also give you a cURL command.

A copied browser request includes everything the browser sent: your session cookie, the user-agent, the origin, and often a short-lived token. That is why the generated code works straight away, and also why it stops working when the session expires. Before sharing the command or committing the code, remove the cookie and tokens or move them into configuration.

Shell quoting, briefly

The same request is written differently for each shell, and the quoting is what most often breaks a command copied between systems:

  • bash and zsh, single quotes: everything inside '…' is literal, including $, " and backslashes. A single quote itself can't appear inside, so it is written '\'' (end the quote, an escaped quote, start again).
  • bash, double quotes: \", \\, \$ and \` are escapes. A real shell would replace $TOKEN with a variable's value; this tool doesn't expand variables, so $TOKEN ends up in the code as literal text for you to replace.
  • bash, $'…': ANSI-C quoting, where \n, \t and \' are escapes. Chrome uses it when a body contains a single quote or a line break.
  • Windows cmd: ^ escapes the next character, and a ^ at the end of a line continues the command on the next line.
  • PowerShell: a backtick at the end of a line continues the command. In Windows PowerShell 5.1, curl is an alias for Invoke-WebRequest, so commands there are written with curl.exe; both spellings are accepted here.

A $ prompt at the start of the command is ignored.

What gets converted

  • Method, URL and headers, including repeated headers and cookies (-b), the user agent (-A) and the referer (-e). A URL without a scheme gets http://, as curl does.
  • Bodies: JSON (--json or a JSON Content-Type) becomes a native object where the language has a neat way to send one (Python json=, JSON.stringify, axios). Form data (-d, --data-urlencode) keeps curl's application/x-www-form-urlencoded type. -G moves the data into the query string.
  • File uploads: -F 'photo=@me.jpg;type=image/jpeg' becomes a multipart form that reads the file, and -d @body.json / -T file read the body from a file.
  • Auth: -u user:pass, --oauth2-bearer and Basic/Bearer Authorization headers.
  • Options: -k (skip certificate checks), -L (follow redirects), -m (timeout) and --compressed. Note that curl only follows redirects with -L, while requests, fetch, axios, Go and .NET follow them by default; Java and PHP get the redirect setting only when the command has -L, and Ruby's Net::HTTP never follows them (a note says so).

Options that only affect curl's own output, such as -s, -v, -i and -o file, are skipped quietly. Headers the library must set itself (Content-Length, Connection, Accept-Encoding, Transfer-Encoding, Expect) are left out, because a copied value would be wrong or would stop the library from decompressing the response. Anything else that can't be converted is listed under the code.

Notes per language

  • JavaScript (fetch) uses top-level await, which works in the browser console, in ES modules and in Node 18+. Browsers don't let a page set Cookie, User-Agent or Referer and only allow cross-site requests the server permits (CORS); Node has neither limit. The code adds a comment when the command contains such headers.
  • Node.js (axios) is an ES module (.mjs); file uploads use fs.openAsBlob (Node 20+). Install with npm install axios.
  • Python uses requests (pip install requests). Repeated headers are joined with commas, because requests takes one value per header.
  • Go, Java (11+, java.net.http), PHP (the cURL extension), C# (.NET 6+ top-level program) and Ruby (Net::HTTP) use only the standard library. Java's HttpClient refuses to set Host, so that header is left out with a note.

Want to send the request first? Paste the same command into the API Request Tester, or benchmark a local API with the Load Tester.

Troubleshooting

"Could not read this command: This doesn't start with "curl"."
The first word must be curl or curl.exe. Remove anything before it, such as a shell prompt other than $, sudo or a comment line.
"Could not read this command: A single quote (') is never closed."
The command was cut off when copying, or a single quote inside a single-quoted bash string wasn't written as '\''. The same message exists for double quotes and $'…' strings.
"Could not read this command: -H needs a value."
An option that takes a value is the last word of the command. This often happens when a line continuation is missing, so the shell would have treated the next line as a separate command.
"-b cookies.txt: cookies from a file can't be imported."
curl reads the cookies from a file on your machine, which this page can't see. Add a -H 'Cookie: name=value' header with the values instead.
The code runs but the server answers 401 or 403
Copied browser requests often depend on a session cookie or a token that has since expired, or on a CSRF header tied to the session. Get a fresh copy, or switch to the API's documented token authentication.

Questions

Why is a JSON body sent as a string in some languages?

Only Python, fetch and axios have a short, idiomatic way to send a native object as JSON. Building the same structure in Go, Java or C# would take many lines of types or maps, so those versions send the original JSON text, which gives the server exactly the same bytes as curl.

What happens to very large numbers in a JSON body?

Python integers have no size limit, so 12345678901234567890 is written as a number. JavaScript numbers lose precision above 253, so in fetch and axios a body containing such a number is kept as the original JSON string instead of being turned into an object.

Is the generated code ready for production?

It is a correct translation of one request, meant as a starting point. It prints the status and body but has no retries or error handling beyond what the language does by default, and secrets from the command are written into the code. Move those to configuration and add the handling your application needs.

Privacy and browser support

Privacy and processing

Everything you type, paste or open is processed by JavaScript in this browser tab. The tool itself makes no network requests, so your input is never sent to DevToolbox or to anyone else.

Settings and saved work stay in this browser only. The privacy policy lists everything the site stores and lets you delete it.

Browser support

Works in current versions of Chrome, Edge, Firefox and Safari, on desktop and on phones.

After your first visit the site can keep an offline copy, so this page also works without a connection.