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

Output language

Converted in your browser — tokens and passwords in the command never leave your device. Tip: in Chrome DevTools, right-click a request in the Network tab → Copy → Copy as cURL.

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.

  1. -H 'Name: value' becomes the headers dict. Content-Type is left out when requests sets it itself — for json= and for form data.
  2. 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 uses data=, just like curl.
  3. The query string moves to params=. Repeated keys (?tag=a&tag=b) become a list of tuples so none are lost.
  4. -F 'file=@photo.jpg' becomes files=, opened in binary mode. Plain -F name=value fields are written as (None, value) so they go as form fields, not files.
  5. -u user:pass becomes auth=(user, pass), -b becomes cookies=, -k becomes verify=False and -x becomes proxies=.
  6. --max-time becomes timeout=. 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 command
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}'
Python requests
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. 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. 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. 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. 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.

Popular uses

Read the guide