Converting cURL commands to Python Requests is a common task for developers. Whether you're integrating APIs, testing endpoints, or building Python applications, knowing how to convert cURL to Python Requests is essential.
In this comprehensive guide, we'll show you how to convert cURL commands to Python Requests with real examples, covering authentication, headers, JSON data, file uploads, and error handling. Use our free cURL to Python Requests converter to automate the process.
Why Convert cURL to Python Requests?
cURL (Command Line)
- Great for testing APIs quickly
- Useful for debugging
- Not suitable for production code
- Limited error handling
Python Requests (Library)
- Perfect for production applications
- Better error handling
- Session management
- Easy to integrate in code
Step-by-Step Conversion Guide
1. Install Python Requests
First, install the Requests library if you haven't already:
pip install requests2. Map cURL Flags to Requests
| cURL Flag | Python Requests |
|---|---|
-X GET | requests.get() |
-X POST | requests.post() |
-H "Header: Value" | headers={"Header": "Value"} |
-d '{"key":"value"}' | json={"key": "value"} |
-u username:password | auth=HTTPBasicAuth('user', 'pass') |
Real-World Examples
Try these examples! Click the copy button to use them in your code. All examples are production-ready.
Basic GET Request
Simple GET request to fetch user data from GitHub API
cURL Command:
curl https://api.github.com/users/octocatPython Requests:
import requests
response = requests.get('https://api.github.com/users/octocat')
print(response.json())POST with JSON Data
POST request with JSON payload using the json parameter
cURL Command:
curl -X POST https://api.example.com/users \
-H "Content-Type: application/json" \
-d '{"name":"John","email":"john@example.com"}'Python Requests:
import requests
data = {
"name": "John",
"email": "john@example.com"
}
response = requests.post(
'https://api.example.com/users',
json=data
)
print(response.json())Basic Authentication
Authenticated request using Basic Auth
cURL Command:
curl -u username:password https://api.example.com/protectedPython Requests:
import requests
from requests.auth import HTTPBasicAuth
response = requests.get(
'https://api.example.com/protected',
auth=HTTPBasicAuth('username', 'password')
)
print(response.json())Bearer Token Authentication
API request with Bearer token authentication
cURL Command:
curl -X GET https://api.example.com/data \
-H "Authorization: Bearer your-token-here"Python Requests:
import requests
headers = {
'Authorization': 'Bearer your-token-here'
}
response = requests.get(
'https://api.example.com/data',
headers=headers
)
print(response.json())Custom Headers
Request with custom headers for API keys and user agents
cURL Command:
curl -X GET https://api.example.com/data \
-H "X-API-Key: abc123" \
-H "User-Agent: MyApp/1.0"Python Requests:
import requests
headers = {
'X-API-Key': 'abc123',
'User-Agent': 'MyApp/1.0'
}
response = requests.get(
'https://api.example.com/data',
headers=headers
)
print(response.json())File Upload (Multipart)
Upload files using multipart/form-data
cURL Command:
curl -X POST https://api.example.com/upload \
-F "file=@/path/to/file.jpg" \
-F "description=Profile picture"Python Requests:
import requests
files = {
'file': ('file.jpg', open('/path/to/file.jpg', 'rb'), 'image/jpeg')
}
data = {
'description': 'Profile picture'
}
response = requests.post(
'https://api.example.com/upload',
files=files,
data=data
)
print(response.json())Best Practices
1. Use Session for Multiple Requests
import requests
session = requests.Session()
session.headers.update({'Authorization': 'Bearer token'})
# Reuse session for multiple requests
response1 = session.get('https://api.example.com/users')
response2 = session.get('https://api.example.com/posts')2. Always Handle Errors
import requests
try:
response = requests.get('https://api.example.com/data')
response.raise_for_status() # Raises exception for bad status codes
data = response.json()
except requests.exceptions.RequestException as e:
print(f'Error: {e}')3. Use json Parameter for JSON Data
Instead of manually setting Content-Type and encoding:
# Good ✅
response = requests.post(url, json={'key': 'value'})
# Avoid ❌
response = requests.post(url, data=json.dumps({'key': 'value'}), headers={'Content-Type': 'application/json'})Convert cURL to Python Requests Instantly
Don't convert manually! Use our free cURL to Python Requests converter to transform any cURL command into production-ready Python code in seconds.