Convert curl to Python Requests
Convert any curl command to Python requests library code. Translate headers, authentication, JSON payloads, and file uploads into clean Pythonic code.
Python
Detailed Explanation
Converting curl to Python Requests
The Python requests library is the most popular HTTP client in the Python ecosystem. Converting curl commands to Python is one of the most common developer tasks.
GET Request
curl:
curl -H "Accept: application/json" https://api.example.com/users
Python:
import requests
response = requests.get(
"https://api.example.com/users",
headers={"Accept": "application/json"}
)
data = response.json()
POST with JSON
curl:
curl -X POST https://api.example.com/users \
-H "Content-Type: application/json" \
-d '{"name": "Alice"}'
Python:
response = requests.post(
"https://api.example.com/users",
json={"name": "Alice"}
)
The json parameter automatically sets the Content-Type header and serializes the dictionary.
Authentication
curl:
curl -u user:pass https://api.example.com/data
Python:
response = requests.get(
"https://api.example.com/data",
auth=("user", "pass")
)
File Upload
curl:
curl -F "file=@photo.jpg" https://api.example.com/upload
Python:
with open("photo.jpg", "rb") as f:
response = requests.post(
"https://api.example.com/upload",
files={"file": f}
)
Key Mapping Reference
| curl flag | Python requests equivalent |
|---|---|
-H |
headers={} dict |
-d |
data= parameter |
-d (JSON) |
json= parameter |
-u |
auth=() tuple |
-F |
files={} dict |
-x |
proxies={} dict |
--timeout |
timeout= seconds |
-L |
Enabled by default |
-k |
verify=False |
Python requests handles redirects, cookies, and connection pooling automatically, making it more concise than raw curl commands for most use cases.
Use Case
A Python developer needs to translate a working curl command from API documentation into production Python code using the requests library.