About curl to python converter
Turn a cURL command into Python code using the requests library, with JSON bodies written as Python dictionaries.
Tips
- Install requests with pip install requests.
- Use json= to send a JSON body and set the content type automatically.
- Always set a timeout, because requests waits forever by default.
After converting
The converter keeps the method, URL, headers and body from your cURL command. Check Python-specific details such as error handling, timeouts and how secrets such as API keys are stored. Never commit real keys to source control.
A worked example
curl -X POST -H 'Content-Type: application/json' -d '{"name":"Asha"}' https://api.example.com/users becomes requests.post('https://api.example.com/users', json={'name': 'Asha'}, timeout=10). Check response.status_code and call response.json() to read the result.
Good habits
- Use a requests.Session when you make many calls to the same host.
- Call response.raise_for_status() to turn error codes into exceptions.
- Read secrets from environment variables, not from the script.
