How to convert curl to Python
- Paste your curl command (from API docs, a terminal or your browser's dev tools).
- The Python code appears instantly, using requests. Switch tabs to see other languages.
- Copy the code and run it after installing the library:
pip install requests.
How curl options map to requests
| curl | Python |
|---|---|
-X POST | requests.post(...) |
-H 'Name: value' | headers={...} |
-d '{"json": true}' | json={...} (a Python dict) |
-d 'a=1&b=2' | data={...} |
-F 'file=@photo.jpg' | files={"file": open("photo.jpg", "rb")} |
-u user:pass | auth=("user", "pass") |
-m 10 | timeout=10 |
-k | verify=False |
Tips for the generated Python code
- Always pass timeout=. requests waits forever by default, so one slow API can hang your script.
- Call response.raise_for_status() to turn 4xx and 5xx responses into exceptions instead of silently using an error body.
- Making many calls to the same API? Use a requests.Session() to reuse connections and shared headers.
Convert curl to other languages
The same converter writes JavaScript, Node.js (axios), Go, PHP and Ruby. See the cURL converter for every supported option.
Need an API to try it on? Every API in our list of APIs with no key works with a plain curl command.