Convert cURL to Python requests
Paste a curl command to get Python requests code — JSON bodies use json=, query strings become params, and a timeout is always set.
AI DevRuns in your browser — nothing uploaded
How curl options map to Python requests
requests does more for you than curl does, so the converted code is often shorter than the command. Here’s where each part goes.
-H 'Name: value'becomes theheadersdict. Content-Type is left out when requests sets it itself — forjson=and for form data.- A JSON body with a JSON Content-Type becomes
json=: requests serialises the dict for you. Without that header curl sends the same text as form data, so the converter usesdata=, just like curl. - The query string moves to
params=. Repeated keys (?tag=a&tag=b) become a list of tuples so none are lost. -F 'file=@photo.jpg'becomesfiles=, opened in binary mode. Plain-F name=valuefields are written as(None, value)so they go as form fields, not files.-u user:passbecomesauth=(user, pass),-bbecomescookies=,-kbecomesverify=Falseand-xbecomesproxies=.--max-timebecomestimeout=. When the command has none, the code uses 30 seconds, because requests waits forever by default.
requests doesn’t raise an error on a 404 or 500 — neither does curl without -f. Call response.raise_for_status() after the request if you want it to.
Example: curl to Python requests
This command, pasted into the converter above, gives the code below.
curl 'https://api.example.com/v1/orders?dry_run=true' \
-H 'Authorization: Bearer YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{"customer_id": 42, "items": [{"sku": "TSHIRT-M", "qty": 2}], "gift": false}'import requests
url = "https://api.example.com/v1/orders"
params = {
"dry_run": "true",
}
headers = {
"Authorization": "Bearer YOUR_API_KEY",
}
json_data = {
"customer_id": 42,
"items": [
{
"sku": "TSHIRT-M",
"qty": 2,
},
],
"gift": False,
}
response = requests.post(
url,
params=params,
headers=headers,
json=json_data,
timeout=30,
)
print(response.status_code)
print(response.text)How it works
- 1
Reads the command like a shell
Single and double quotes, $'…' strings, backslash line breaks and Windows ^ escapes are all understood — so Chrome’s “Copy as cURL (bash)” and “(cmd)” both work.
- 2
Understands curl’s options
-X, -H, -d, --data-raw, --data-urlencode, --json, -F, -u, -b, -G, -k, -L, -x, --max-time and more — with curl’s own defaults, like POST when there’s a body.
- 3
Writes idiomatic code
json= in Python, URLSearchParams and FormData in JavaScript, a multipart writer in Go, CURLFile in PHP and a text block in Java — the way each language would normally do it, using only what ships with it where possible.
- 4
Tested against real curl
Code in all seven languages was run against a test server and checked to send the same method, URL, headers and body as curl itself.
Questions, answered
json= takes a Python dict or list, turns it into JSON and sets Content-Type: application/json. data= sends a dict as form fields (application/x-www-form-urlencoded) or a string exactly as given. Use json= for JSON APIs.
Yes, requests isn’t part of the standard library: run pip install requests. It works with any Python 3 version that’s still supported.
Mostly by renaming requests to httpx — headers=, params=, json=, data=, files= and auth= work the same way. Two differences: httpx doesn’t follow redirects unless you pass follow_redirects=True, and it uses proxy= instead of proxies=.
Often it’s the User-Agent: requests sends python-requests/x.y, which some sites block. Copy the User-Agent header from the original command into headers. Expired cookies or tokens copied from the browser are the other common cause.
Pass it in files=, opened in binary mode: files={"file": open("photo.jpg", "rb")}. To set the filename or content type, use a tuple: ("photo.jpg", open("photo.jpg", "rb"), "image/jpeg"). The converter writes this for you from curl -F.
Popular uses
Read the guide
Related tools
- Prompt CompressorCut tokens from prompts and code before you send them to an LLM.AI Dev
- Regex ExplainerPaste a regex, get plain English plus test cases.AI Dev
- AI SQL Query GeneratorEnglish to SQL for Postgres, MySQL and MSSQL.AI Dev
- JSON to Pydantic / TSInstant Pydantic models, TypeScript types or dataclasses.AI Dev