How to Convert cURL Commands to Python requests — Complete Guide with Code
Step-by-step guide to converting cURL commands into clean, production-ready Python requests code. Covers GET, POST, JSON, custom headers, auth, and error handling.
Interactive cURL to Python Translator
Live Interactive SandboxTranslates shell flags into clean requests.post dictionary arguments. Test the preset input below or customize it before launching into the full workspace.
cURL is the universal language of API documentation and network debugging. When you copy an API request from Chrome DevTools, Postman, or Swagger, it almost always comes formatted as a curl command.
However, when building automation scripts or backend services, you need to convert that command into clean, readable Python code using the standard requests library.
This guide provides a comprehensive mapping of all cURL parameters to Python requests.
1. Basic GET Request
cURL:
curl https://api.example.com/v1/users
Python:
import requests
response = requests.get('https://api.example.com/v1/users')
print(f"Status Code: {response.status_code}")
data = response.json()
print(data)
2. Query Parameters
Instead of manually constructing URL query strings, pass a dictionary to the params argument in Python.
cURL:
curl "https://api.example.com/v1/search?query=developer&limit=20&sort=desc"
Python:
import requests
params = {
'query': 'developer',
'limit': 20,
'sort': 'desc'
}
response = requests.get('https://api.example.com/v1/search', params=params)
print(response.url)
3. POST Requests with JSON Payloads (-d / --data)
When sending JSON data in Python, use the json= parameter rather than data=:
import requests
payload = {
'name': 'Saad',
'role': 'engineer',
'active': True
}
response = requests.post('https://api.example.com/v1/users', json=payload)
print(response.status_code)
Live Tool: cURL to Python Requests
Client-Side EngineTranslate bash cURL commands into Python `requests` code.