How to use the cURL to Python Converter
- Paste a curl command into the left box (from API docs or Copy as cURL in your browser).
- Python requests is already selected.
- Check the notes under the code for anything that needs your attention, like files to load.
- Click Copy, then run it after
pip install requests.
How curl options become requests arguments
| curl | requests |
|---|---|
-H 'Name: value' | headers={"Name": "value"} |
-d '{"a": 1}' with a JSON content type | json={"a": 1} (requests sets the Content-Type header for you) |
-d 'a=1&b=2' | data={"a": "1", "b": "2"} |
-F '[email protected]' | files={"file": ("photo.jpg", open("photo.jpg", "rb"))} |
?page=2&q=thai in the URL, or -G -d | params={"page": "2", "q": "thai"} |
-u user:pass | auth=("user", "pass") |
-b 'session=abc' | cookies={"session": "abc"} |
-m 10 | timeout=10 |
-k | verify=False |
Tips for the generated code
- Always set a timeout in real scripts. requests waits forever by default, which can hang a job.
- Check errors with
response.raise_for_status()before readingresponse.json(). - Reuse connections: for many calls to the same API, create a
requests.Session()and callsession.get(...). It keeps the connection and cookies. - Keep secrets out of the code: move tokens to environment variables with
os.environ["API_TOKEN"]. - Async code?
httpxhas almost the same API: changerequests.posttohttpx.post, or usehttpx.AsyncClient.
Frequently asked questions
Does requests follow redirects like curl -L?
Yes, requests follows redirects by default, while curl only does with -L. Add allow_redirects=False if you need curl's default behaviour.
Why is my -F form sent with files=?
curl -F always sends multipart/form-data. In requests, only files= does that, so plain fields are written as (None, value) pairs.
Does it work with Python 2?
The code uses requests features that work on Python 3.8 and later. Python 2 is no longer supported by requests.
Is my command uploaded?
No, it's converted in your browser.
Updated