cURL to Python Converter
Convert cURL commands to Python requests code — paste and get clean Python instantly
You have a working cURL command from API documentation or your browser’s DevTools Network tab and need to call the same endpoint from Python. Translating headers, body, and authentication flags into idiomatic Python is repetitive work. This converter does it instantly — paste the cURL command and get clean Python code using the requests library.
Why Python requests?
The requests library is the de facto standard for HTTP in Python. It provides a clean, intuitive API that abstracts away the complexity of Python’s built-in urllib. With over 50 million monthly downloads on PyPI, it is installed in virtually every Python environment and accepted as the idiomatic choice for API interactions in scripts, data pipelines, and web scrapers.
Install it with:
pip install requests
How the Conversion Maps curl to Python
| cURL flag | Python requests equivalent |
|---|---|
-X POST | requests.post(...) |
-H "Content-Type: application/json" | headers={"Content-Type": "application/json"} |
-d '{"key":"value"}' | json=json.loads('{"key":"value"}') |
-u user:pass | auth=("user", "pass") |
-b "session=abc" | cookies={"Cookie": "session=abc"} |
-k / --insecure | verify=False |
-L / --location | handled automatically by requests |
JSON body detection is automatic: if your cURL command’s body starts with { or [, or if the Content-Type header is application/json, the converter uses the json= parameter instead of data=. This tells requests to serialize the dict and set the correct content type header automatically.
Handling Authentication
Basic auth — The -u user:password flag becomes auth=("user", "password"). The requests library encodes this as a base64 Authorization header automatically.
Bearer tokens — Authorization headers passed via -H "Authorization: Bearer TOKEN" are included in the headers dict as-is.
API keys in headers — Any -H "X-API-Key: secret" flag is preserved in the generated headers dict.
Sessions for Multiple Requests
The generated code makes a single request. If you need to make multiple requests to the same API — for example, to maintain a login session or reuse connection pooling — switch the output to use a Session object:
session = requests.Session()
session.headers.update(headers)
response = session.get("https://api.example.com/data")
Sessions also persist cookies across requests automatically, which is useful for scraping or authenticated multi-step flows.
Handling the Response
The generated code prints response.status_code and response.text. For JSON APIs, use response.json() to parse directly into a Python dict:
data = response.json()
print(data["key"])
Always check response.raise_for_status() in production code to raise an exception automatically on 4xx and 5xx responses.
SSL and Proxies
If your cURL command uses -k or --insecure, the Python output sets verify=False with a warning comment. Disable SSL verification only for development against self-signed certificates — never in production. For production, pass verify="/path/to/ca-bundle.crt" to use a custom certificate authority.
Frequently Asked Questions
Why does my Python code get a different response than curl?
The most common cause is a missing or mismatched User-Agent header. APIs sometimes serve different content to known scraper agents. Set headers={"User-Agent": "Mozilla/5.0 ..."} to mimic a browser if needed.
How do I handle rate limiting in the generated code?
Add a time.sleep() between requests and check for response.status_code == 429. Use the Retry-After response header value as the sleep duration if the API provides it.
Can I use the output with httpx instead of requests?
The httpx library has a largely compatible API. Replace import requests with import httpx and requests.get(...) with httpx.get(...). The named parameters (headers=, json=, auth=) work identically in most cases.