Convert cURL to Python (requests)
Paste a curl command to get the same request as Python requests code. Below the converter, each flag it understands is shown with the exact lines it adds, and the flags that need a line of your own in Python are spelled out.
What each curl flag becomes in Python requests
Each example below is the converter's real output, generated when this page was built, from the same code the box above runs. Only the lines a flag adds are shown, compared with converting a bare curl URL, so you can see exactly where each piece of the command lands in Python.
-H · Request headers
curl
curl https://api.example.com/items -H "Accept: application/json" -H "X-Request-Id: 42"
Lines it adds to the Python requests code
headers = {
"Accept": "application/json",
"X-Request-Id": "42",
}
…
response = requests.get(url, headers=headers)
--json · A JSON body
--json is curl shorthand for a body plus both Content-Type: application/json and Accept: application/json, so both headers appear.
curl
curl https://api.example.com/items --json '{"name":"widget","qty":2}'
Lines it adds to the Python requests code
headers = {
"Content-Type": "application/json",
"Accept": "application/json",
}
…
payload = {
"name": "widget",
"qty": 2
}
response = requests.post(url, headers=headers, json=payload)
-d · A form body
Two -d flags join with &, any -d makes the request a POST, and curl labels the body application/x-www-form-urlencoded unless you set a Content-Type yourself.
curl
curl https://api.example.com/login -d "user=ada" -d "remember=1"
Lines it adds to the Python requests code
headers = {
"Content-Type": "application/x-www-form-urlencoded",
}
…
data = "user=ada&remember=1"
response = requests.post(url, headers=headers, data=data)
-G · Data sent as query parameters
-G moves the data into the URL and sends a GET with no body and no Content-Type, which is what curl puts on the wire.
curl
curl -G https://api.example.com/search --data-urlencode "q=café au lait"
Lines it adds to the Python requests code
url = "https://api.example.com/search?q=caf%C3%A9+au+lait"
-u · Basic authentication
-u becomes an Authorization: Basic header with the base64 of user:password. If the command also sets its own Authorization header, that header wins, as it does in curl.
curl
curl -u ada:s3cret https://api.example.com/me
Lines it adds to the Python requests code
headers = {
"Authorization": "Basic YWRhOnMzY3JldA==",
}
…
response = requests.get(url, headers=headers)
-b · Cookies
curl
curl -b "session=abc123; theme=dark" https://api.example.com/me
Lines it adds to the Python requests code
headers = {
"Cookie": "session=abc123; theme=dark",
}
…
response = requests.get(url, headers=headers)
-F · A multipart upload
curl
curl https://api.example.com/upload -F "title=Q3 report" -F "file=@report.pdf"
Lines it adds to the Python requests code
form_data = {
"title": "Q3 report"
}
# open() in binary mode; requests streams the file for you and sets
# the multipart Content-Type with a fresh boundary.
files = {
"file": (
"report.pdf",
open("report.pdf", "rb"),
"application/octet-stream",
),
}
response = requests.post(url, data=form_data, files=files)
The converter also notes: a -F upload needs Content-Type: multipart/form-data with a boundary generated per request, so the target language sets that header itself.
-X · A different method
curl
curl -X DELETE https://api.example.com/items/7
Lines it adds to the Python requests code
response = requests.delete(url)
-I · A HEAD request
curl
curl -I https://api.example.com/health
Lines it adds to the Python requests code
response = requests.head(url)
What the generated code does not carry over
Some curl flags change how the transfer behaves rather than what the request is, and the converter leaves them out. For each flag below, this build checked that adding it leaves the Python requests output unchanged, then noted what requests does by default and how to match curl:
-L: Nothing to add: requests follows redirects by default for every method except HEAD. The difference runs the other way. A command without-Lstops at the 3xx response, and to reproduce that you passallow_redirects=False.--compressed: Nothing to add: requests already asks for gzip and deflate and decodes the body for you (Brotli too, if thebrotlipackage is installed).-k: Passverify=False. It turns off certificate checking for that request, so keep it to a test server you control.-m 10: Passtimeout=10. Without it requests waits indefinitely. Note that it bounds the wait for each read rather than the whole download, while-mcaps the entire transfer.
More from cURLParse
Guides
Frequently Asked Questions
Why does my requests code follow a redirect that curl did not?
Because requests follows redirects by default for every method except HEAD, while curl stops at the 3xx response unless you pass -L. The converter leaves -L out, since requests already behaves that way. To get the behavior of a curl command without -L, pass allow_redirects=False.
Why is there no timeout in the generated Python code?
curl -m is a transfer setting rather than part of the request, so the converter does not translate it. Add timeout=10 to the requests call. Without a timeout, requests can wait indefinitely; with one, it limits each wait for data rather than the whole download.