cURL to Python: from curl to code
Paste a curl command, the one you copied from the browser's network tab or from an API reference, and get the Python code that makes the same request with the requests library. Headers become a dict, a JSON body becomes the json= argument (so requests sets the Content-Type), -u becomes auth=(user, password) and -F becomes files= and data=.
This page opens the converter on Python requests. The command is only read and turned into text: nothing is run and nothing is sent from the page.
A real conversion
The tool opens with this command, a POST of a JSON order with a bearer token:
curl -X POST 'https://api.example.com/v1/orders?expand=items' \
-H 'Authorization: Bearer sk_test_4eC39HqLyjWDarjtT1zdp7dc' \
-H 'Content-Type: application/json' \
-d '{"customer":"ana@example.com","items":[{"sku":"KB-01","qty":2}],"coupon":null}'and this is what it writes for Python requests:
import requests
headers = {
"Authorization": "Bearer sk_test_4eC39HqLyjWDarjtT1zdp7dc",
"Content-Type": "application/json",
}
payload = {
"customer": "ana@example.com",
"items": [
{
"sku": "KB-01",
"qty": 2,
},
],
"coupon": None,
}
response = requests.request(
"POST",
"https://api.example.com/v1/orders?expand=items",
headers=headers,
json=payload,
allow_redirects=False,
)
print(response.status_code)
print(response.text)Which flags become what
Starting from a plain GET of https://api.example.com/items, each flag below adds this to the generated code (computed with the converter itself):
| Flag | What it is | First new line |
|---|---|---|
-H 'Accept: application/json' | a request header | headers = { |
-d 'a=1&b=2' | a body (the method becomes POST) | headers = { |
-u ana:secret | basic authentication | auth=("ana", "secret"), |
-F 'photo=@a.png' | a multipart file upload | files = { |
-X DELETE | another method | "DELETE", |
Another command
curl -u admin:s3cret 'https://api.example.com/items?limit=10' -H 'Accept: application/json'import requests
headers = {
"Accept": "application/json",
}
response = requests.request(
"GET",
"https://api.example.com/items?limit=10",
headers=headers,
auth=("admin", "s3cret"),
allow_redirects=False,
)
print(response.status_code)
print(response.text)Basic authentication and a query string: the user and password go to auth=, and the URL keeps its query. Never commit a script with a real secret in it; read it from an environment variable.
Running it
pip install requests
python request.pyCommon mistakes
- Pasting a command with a real token and then sharing the Python: the token is now in the code. Replace it with os.environ["TOKEN"] before sharing.
- Forgetting that curl does not follow redirects unless you pass -L: the generated code sets allow_redirects=False for the same reason, so a 301 is returned as it is.
How to use the converter
- Copy the command: from the browser (network tab, Copy as cURL), from an API reference or from your terminal history.
- Paste it in the box; lines that end with a backslash are joined, so a multi-line command works.
- Copy the code or download it as a file.
Frequently asked questions
How do I convert curl to Python requests?
Paste the command in the box and copy the Python on the right. Install requests with pip and run the file.
Why is the body sent with json= and not data=?
When the body is valid JSON the tool writes it as a Python dict and passes json=, which serialises it and sets the right header. Form bodies (a=1&b=2) use data=.
Does it run the request?
No: the command is only read and converted to text. Nothing is sent from this page; the request happens only when you run the Python on your machine.