Back to Tools

Convert cURL to Python Requests — Free Online Converter

Paste a cURL command, get Python code with the requests library. Headers, auth, JSON, and files supported.

Select Python (Requests) or Python (HTTPX) in the target dropdown, then paste your cURL and convert.

All processing happens locally in your browser.

Your API keys or requests are never sent to any server.

cURL to Code Converter

Convert cURL to production-ready code in JavaScript (Fetch/Axios), Python (Requests/HTTPX), Go, Java, PHP, C#, Rust. Export to Postman & OpenAPI.

How to convert cURL to Python requests

Use the converter above: paste your cURL command, choose Python (Requests), and click convert. The tool maps cURL options to Python requests calls so you get runnable code with the same URL, method, headers, and body.

If you have many cURL commands or need other languages (JavaScript, Go, Java, PHP), use our full cURL to Code Converter. For step-by-step guides and examples, see cURL to Python Requests: Complete Guide.

cURL vs Python requests — syntax comparison

cURL uses flags and positional args; Python requests uses method calls and keyword arguments. The converter handles the mapping so you don't have to.

cURLPython requests
-X POSTrequests.post(url, ...)
-H "Content-Type: application/json"headers={'Content-Type': 'application/json'}
-d '{"key":"val"}'json={'key': 'val'}
-u user:passauth=('user', 'pass')
-H "Authorization: Bearer TOKEN"headers={'Authorization': 'Bearer TOKEN'}

Common cURL flags and their Python equivalent

  • -X METHODrequests.get/post/put/delete/patch(url, ...)
  • -H "Name: value"headers={'Name': 'value'}
  • -d '...'data=... or json=... for JSON
  • -u user:passauth=('user', 'pass')
  • --connect-timeout Ntimeout=N

Code examples: GET, POST, headers, auth

After converting, you get Python like this. For GET with headers:

import requests
url = "https://api.example.com/data"
headers = {"Authorization": "Bearer YOUR_TOKEN"}
r = requests.get(url, headers=headers)
print(r.json())

For POST with JSON body:

import requests
url = "https://api.example.com/users"
headers = {"Content-Type": "application/json"}
payload = {"name": "John", "email": "john@example.com"}
r = requests.post(url, json=payload, headers=headers)
print(r.status_code)

The converter above fills in url, headers, and body from your cURL so you can run or tweak the code immediately. For more examples and edge cases, use our cURL to Python Requests tool page or the full cURL Converter.

Why convert cURL to Python?

cURL is ideal for one-off tests in the terminal. Python with requests is better for scripts, automation, and production code: you get loops, error handling, and integration with the rest of your app. Converting cURL to Python lets you keep the exact request (URL, headers, body) while moving from the command line into code.

This converter is free, runs in your browser, and doesn't store your commands. For HAR-based conversion (browser network export to cURL then to code), use our HAR to cURL tool first, then paste the generated cURL here.

FAQ

Frequently Asked Questions

1How do I convert a curl command to Python?
Paste your curl command into the input box above and the tool instantly generates the equivalent Python requests code. All flags are parsed automatically: -H headers become a headers dict, -d becomes json= or data=, -u becomes auth=, and -X sets the HTTP method.
2Does the converter support all curl flags?
The converter handles the most common flags used in API calls: -X (method), -H (headers), -d and --data-raw (body), -u (basic auth), -b (cookies), -k (SSL skip), -L (follow redirects), --compressed, and --max-time (timeout). Complex flags like --cert for client certificates may require manual adjustment.
3How do I send JSON in Python requests?
Use the json= parameter: requests.post(url, json={'key': 'value'}, headers=headers). Python requests automatically sets Content-Type: application/json and serializes the dict. Alternatively use data=json.dumps({...}) with a manual Content-Type header for more control.
4How do I add Authorization headers in Python requests?
Pass a headers dict: requests.get(url, headers={'Authorization': 'Bearer YOUR_TOKEN'}). For Basic auth, use the auth parameter: requests.get(url, auth=('username', 'password')). The converter translates both -H Authorization and -u flags into the correct Python pattern.
5How do I handle SSL verification in Python requests?
By default Python requests verifies SSL certificates. To disable (equivalent to curl -k), pass verify=False. For production, never disable SSL — instead point to a custom CA bundle: verify='/path/to/ca-bundle.crt'.
6How do I set a timeout in Python requests?
Pass a timeout parameter in seconds: requests.get(url, timeout=30). Set separate connect and read timeouts as a tuple: requests.get(url, timeout=(5, 30)). Without a timeout, requests will hang indefinitely if the server does not respond.
7How do I send form data in Python requests?
Use data= with a dict for application/x-www-form-urlencoded: requests.post(url, data={'field': 'value'}). For multipart/form-data file uploads, use files= instead. Python requests sets the correct Content-Type automatically for both.
8How do I upload a file with Python requests?
Open the file in binary mode and pass it via files=: with open('file.pdf', 'rb') as f: requests.post(url, files={'file': f}). Combine files= and data= parameters to include additional form fields alongside the file.
9How do I follow redirects in Python requests?
Python requests follows redirects automatically by default (equivalent to curl -L). To disable this, pass allow_redirects=False. Check response.history to see the list of intermediate responses in the redirect chain.
10How do I handle cookies in Python requests?
Pass cookies as a dict: requests.get(url, cookies={'session': 'abc123'}). To persist cookies across requests like a logged-in session, use a Session object: s = requests.Session(). The session automatically stores and sends cookies between requests.
11What is the difference between requests.get() and requests.post()?
requests.get() retrieves data and accepts query parameters via params=. requests.post() sends data to create or update a resource and accepts a request body via json=, data=, or files=. Use whichever HTTP method the API endpoint requires.
12How do I debug a Python requests error?
Check response.status_code and response.text to see what the server returned. Enable verbose logging with logging.basicConfig(level=logging.DEBUG). Inspect response.request.headers and response.request.body to verify exactly what was sent.