How to Convert cURL to Python requests — Complete Step-by-Step Guide
You copied a cURL command from Chrome DevTools, Postman, or API documentation — and now you need it as Python code. This guide shows you exactly how to convert every cURL pattern to Python requests: GET, POST with JSON, form data, auth headers, cookies, sessions, and file uploads. Plus the free online converter that does it in one paste.
requests
Most popular Python HTTP library — 50M+ downloads/week
< 1 min
Convert any cURL to working Python with the online tool
15+
cURL flags that have a direct Python requests equivalent
Why Convert cURL to Python?
cURL is the universal language for HTTP requests — API documentation, browser DevTools, Postman, and backend logs all produce cURL commands. But if you are building a Python script, a data pipeline, a test suite, or a web scraper, you need the request in Python. Translating cURL to Python by hand takes time and is easy to get wrong, especially when dealing with multi-line headers, JSON bodies, and auth tokens.
API docs give you cURL examples
Nearly every API — Stripe, OpenAI, Twilio, GitHub — shows curl examples first. To use them in Python, you need to convert them.
Chrome DevTools exports cURL
Right-click any browser network request → Copy → Copy as cURL. Perfect for reproducing requests in automation scripts.
Postman can copy as cURL
Code → cURL gives you the exact request. From there, converting to Python lets you use it in scripts without Postman installed.
Bash scripts use cURL
Legacy shell scripts often use cURL for API calls. Migrating them to Python requires converting each cURL command.
Convert instantly without reading the whole guide
Convert cURL GET to Python requests.get()
The simplest case — a basic GET request. The URL goes in the first argument. Headers become a dictionary passed to the headers parameter.
curl 'https://api.example.com/users' -H 'Accept: application/json' -H 'Authorization: Bearer eyJhbGciOiJSUzI1NiJ9...'import requests
url = 'https://api.example.com/users'
headers = {
'Accept': 'application/json',
'Authorization': 'Bearer eyJhbGciOiJSUzI1NiJ9...',
}
response = requests.get(url, headers=headers)
print(response.status_code) # 200
print(response.json()) # parsed response bodyQuery parameters
If the cURL URL has query parameters like ?page=2&limit=50, you can either leave them in the URL string or pass them as a params dict: requests.get(url, params={'page': 2, 'limit': 50}). The dict form is cleaner and handles URL encoding automatically.
Convert cURL POST with JSON Body to Python
POST requests with JSON bodies are the most common API call pattern. The -d or --data cURL flag maps to the json parameter in Python requests — which automatically sets the Content-Type: application/json header.
curl -X POST 'https://api.example.com/users' -H 'Content-Type: application/json' -H 'Authorization: Bearer TOKEN' -d '{"name": "Alice", "email": "alice@example.com", "role": "admin"}'import requests
url = 'https://api.example.com/users'
headers = {
'Authorization': 'Bearer TOKEN',
}
payload = {
'name': 'Alice',
'email': 'alice@example.com',
'role': 'admin',
}
response = requests.post(url, json=payload, headers=headers)
print(response.status_code) # 201
print(response.json()) # {'id': 42, 'name': 'Alice', ...}json= vs data= in Python requests
json=payload when sending JSON (it auto-sets Content-Type and serializes the dict). Use data=payload when sending form data (application/x-www-form-urlencoded). Use data=string when you need to send a raw string body exactly as-is.Convert cURL with Bearer Token Auth to Python
Authorization headers from cURL translate directly to the headers dict. For Bearer tokens specifically, Python requests also has an auth parameter for Basic auth — but for Bearer tokens, you always use the header dict approach.
Bearer token auth — wrong vs. correct Python translation
Common mistake
# Wrong: auth= is for Basic auth only (username/password)
response = requests.get(url, auth='Bearer TOKEN')Correct approach
# Correct: Bearer token goes in the Authorization header
headers = {'Authorization': 'Bearer TOKEN'}
response = requests.get(url, headers=headers)
# For Basic auth (curl -u user:pass), use HTTPBasicAuth:
from requests.auth import HTTPBasicAuth
response = requests.get(url, auth=HTTPBasicAuth('user', 'pass'))# Bearer token
curl 'https://api.example.com/data' -H 'Authorization: Bearer TOKEN'
# Basic auth
curl 'https://api.example.com/data' -u 'username:password'
# API key in header
curl 'https://api.example.com/data' -H 'X-API-Key: my-secret-key'
# API key in query string
curl 'https://api.example.com/data?api_key=my-secret-key'import requests
from requests.auth import HTTPBasicAuth
# Bearer token
response = requests.get(url, headers={'Authorization': 'Bearer TOKEN'})
# Basic auth (curl -u username:password)
response = requests.get(url, auth=HTTPBasicAuth('username', 'password'))
# or shorthand:
response = requests.get(url, auth=('username', 'password'))
# API key in header
response = requests.get(url, headers={'X-API-Key': 'my-secret-key'})
# API key in query string
response = requests.get(url, params={'api_key': 'my-secret-key'})Convert cURL Form Data (--data-urlencode) to Python
When cURL sends form data with --data-urlencode or -F (multipart), the Python equivalent uses the data or files parameter respectively.
# URL-encoded form data (application/x-www-form-urlencoded)
curl -X POST 'https://api.example.com/login' --data-urlencode 'username=alice' --data-urlencode 'password=s3cr3t'
# Multipart form data (file + field together)
curl -X POST 'https://api.example.com/upload' -F 'file=@/path/to/document.pdf' -F 'title=My Document'import requests
# URL-encoded form data
response = requests.post(
'https://api.example.com/login',
data={'username': 'alice', 'password': 's3cr3t'}
)
# Multipart form data with file upload
with open('/path/to/document.pdf', 'rb') as f:
response = requests.post(
'https://api.example.com/upload',
files={'file': ('document.pdf', f, 'application/pdf')},
data={'title': 'My Document'},
)
print(response.status_code)Convert cURL with Cookies to Python
The --cookie or -b cURL flag passes cookies. In Python requests, cookies can go in the cookies parameter as a dict or as a RequestsCookieJar. For multi-request sessions that preserve cookies automatically (like browser sessions), use requests.Session().
curl 'https://app.example.com/dashboard' -H 'Accept: application/json' --cookie 'session_id=abc123xyz; csrf_token=tok789'import requests
# Single request — pass cookies as dict
response = requests.get(
'https://app.example.com/dashboard',
headers={'Accept': 'application/json'},
cookies={'session_id': 'abc123xyz', 'csrf_token': 'tok789'},
)
# Session — cookies persist across requests automatically
session = requests.Session()
session.cookies.update({'session_id': 'abc123xyz', 'csrf_token': 'tok789'})
# All subsequent requests reuse the session cookies
r1 = session.get('https://app.example.com/dashboard')
r2 = session.get('https://app.example.com/profile')
r3 = session.post('https://app.example.com/settings', json={'theme': 'dark'})Convert cURL PUT, PATCH, DELETE to Python
The -X flag in cURL specifies the HTTP method. Python requests has a method for each: requests.put(), requests.patch(), requests.delete().
# PUT — full update
curl -X PUT 'https://api.example.com/users/42' -H 'Content-Type: application/json' -H 'Authorization: Bearer TOKEN' -d '{"name": "Alice Smith", "email": "alice@new.com"}'
# PATCH — partial update
curl -X PATCH 'https://api.example.com/users/42' -H 'Content-Type: application/json' -H 'Authorization: Bearer TOKEN' -d '{"email": "alice@new.com"}'
# DELETE
curl -X DELETE 'https://api.example.com/users/42' -H 'Authorization: Bearer TOKEN'import requests
headers = {'Authorization': 'Bearer TOKEN'}
base_url = 'https://api.example.com/users/42'
# PUT — full update
response = requests.put(
base_url,
json={'name': 'Alice Smith', 'email': 'alice@new.com'},
headers=headers,
)
# PATCH — partial update
response = requests.patch(
base_url,
json={'email': 'alice@new.com'},
headers=headers,
)
# DELETE
response = requests.delete(base_url, headers=headers)
print(response.status_code) # 204 No ContentHandle cURL --compressed, --insecure, and Timeouts
Some cURL flags have important Python equivalents that are easy to forget.
--compressed → automatic in Python
Python requests automatically decompresses gzip and deflate responses. You do not need to add anything — the Accept-Encoding header is sent and decompression happens transparently.
-k / --insecure → verify=False
If cURL uses -k to skip SSL verification: requests.get(url, verify=False). Add warnings.filterwarnings("ignore") to suppress the InsecureRequestWarning in production. Never use this in production.
--connect-timeout → timeout parameter
requests.get(url, timeout=10) sets a 10-second connect+read timeout. Use a tuple for separate connect/read timeouts: timeout=(3.05, 27). Always set a timeout — the default is no timeout (hangs forever).
-x / --proxy → proxies parameter
proxies={"http": "http://proxy:8080", "https": "http://proxy:8080"}. For SOCKS5 proxies (curl --socks5): proxies={"https": "socks5://proxy:1080"} with the requests[socks] extra.
import requests
import warnings
# --compressed → automatic, no action needed
response = requests.get('https://api.example.com/data')
# -k / --insecure → verify=False (dev/testing only)
with warnings.catch_warnings():
warnings.simplefilter('ignore')
response = requests.get('https://internal.corp.dev/api', verify=False)
# --connect-timeout 5 → timeout=5
response = requests.get(url, timeout=5)
# Separate connect (3s) and read (30s) timeout
response = requests.get(url, timeout=(3, 30))
# -x http://proxy:8080 → proxies
proxies = {'http': 'http://proxy:8080', 'https': 'http://proxy:8080'}
response = requests.get(url, proxies=proxies)
# --max-redirs 0 / --location-trusted → allow_redirects
response = requests.get(url, allow_redirects=False) # follow=FalseUse the Online cURL to Python Converter
For any cURL command — especially long ones from Chrome DevTools with 20+ headers — the fastest approach is to use the free online converter. Paste the cURL, get working Python instantly, no setup required.
Copy the cURL command
From Chrome DevTools (right-click request → Copy as cURL), from API docs, from Postman (Code → cURL), or from any other source.
Paste into the converter
Go to unblockdevs.com/curl-to-python and paste the cURL command into the input panel. The tool parses the command immediately.
Review the Python code
The converter generates clean Python requests code with a headers dict, a JSON or form data payload, the correct method (GET/POST/PUT/PATCH/DELETE), and proper auth.
Copy and use in your project
Click the copy button and paste into your Python script, Jupyter notebook, or test file. The code runs immediately — no modifications needed for most requests.
Chrome DevTools cURL → Python in 30 seconds