cURL converter · Python cURL to Python: convert curl commands to requests
Paste a curl command from API docs or your browser and get Python code that uses the requests library: the right method, a headers dict, json= for JSON bodies, data= for forms, files= for uploads, plus auth, cookies, verify and timeout.
- requests library
- Bash and cmd quoting
- Parsed locally
Convert curl to Python requests
The output language is set to Python. Paste your own command; the code updates as you type.
Line breaks with \ (bash), ^ (cmd) or ` (PowerShell) are fine. Variables like $TOKEN are kept as text.
Checks
- WarningContains credentials (Authorization). They stay in this page, but remove them before sharing the code or committing it; load them from environment variables instead.
import requests
headers = {
'Authorization': 'Bearer wck_…',
}
json_data = {
'events': [
{
'event': 'purchase',
'login_id': 'u_123',
'insert_id': 'order-1001',
'properties': {
'revenue': 19.9,
},
},
],
}
response = requests.post(
'https://api.pathclue.com/api/v1/track',
headers=headers,
json=json_data,
)
print(response.status_code)
print(response.text)Parsed request
What the command sends, so you can check the conversion.
| Part | Value |
|---|---|
| Method | POST |
| URL | https://api.pathclue.com/api/v1/track |
| Header Authorization | Bearer wck_… |
| Header Content-Type | application/json |
| Body | JSON |
Version v1.0Updated Update history
Check the conversion in three places
Code that looks right can still send a different request. These are the parts worth a second look.
Parsed request
The table under the code lists the method, URL, every header and the body type exactly as curl would send them. If something is missing there, the command was cut off or quoted differently than you think.
Method
curl switches to POST when you add -d or -F, and to GET with -G. The sample has an explicit -X POST, so the code uses POST either way.
Body type
A JSON body becomes a native object (json= in Python, JSON.stringify in JavaScript, an array in PHP), so you can edit it as data. Form data becomes field pairs; -F becomes a multipart upload.
Checks
Warnings flag credentials in the command, disabled certificate checks, files the code has to read, and options that could not be converted.
The sample key wck_… is the placeholder from our docs, which is why the credentials warning appears. A real key would trigger the same warning.
Three steps
Nothing is run and nothing is sent.
-
1
Copy the command
From API docs, a terminal, or your browser: in the Network tab, right-click a request and choose Copy as cURL (bash or cmd both work).
-
2
Pick the language
fetch for front-end code, Node.js for scripts and servers, or Python, PHP, Go or Java.
-
3
Copy and clean up
Copy the code, move tokens and cookies into environment variables, and remove browser-only headers you do not need.
What the converter understands
The parser follows the curl manual. Options that only change curl’s own output (-s, -v, -o, -i) are ignored because they do not change the request.
- Quoting: single and double quotes,
$'…', backslash escapes and line continuations in bash;^in Windows cmd; backtick in PowerShell - Short options can be combined (
-sSL) or attached (-XPOST) - Without a scheme, a URL gets
http://, as curl does
| curl option | Meaning | In Python requests |
|---|---|---|
-X, -I, -G | Method; HEAD; data goes into the query string | requests.post(…), requests.head(…), query appended to the URL |
-H, -A, -e | Header; User-Agent; Referer | headers={…} |
-d, --data-raw, --data-binary, --data-urlencode | Body, form-encoded by default | data={…} or data='…' |
--json, or -d with a JSON Content-Type | JSON body | json={…} |
-F, --form-string | multipart/form-data, @file uploads a file | files={…} |
-u, -b | Basic auth; cookies | auth=(…), cookies={…} |
-L, -k, -m, --compressed | Follow redirects; skip TLS checks; timeout; accept gzip | Followed by default, verify=False, timeout=, automatic |
One request, six languages
How the sample command (a JSON POST with a bearer token) is written in each language.
| Language | Request call | JSON body | Headers |
|---|---|---|---|
| JavaScript (fetch) | await fetch(url, { method: 'POST', … }) | body: JSON.stringify({…}) | headers: {…} |
| Node.js | Same as fetch (built in since Node 18) | body: JSON.stringify({…}) | headers: {…} |
| Python | requests.post(url, …) | json=json_data | headers=headers (Content-Type left to requests) |
| PHP | curl_setopt_array($ch, […]) | CURLOPT_POSTFIELDS => json_encode([…]) | CURLOPT_HTTPHEADER |
| Go | http.NewRequest("POST", url, body) | strings.NewReader(`{…}`) | req.Header.Set(…) |
| Java | HttpRequest.newBuilder().POST(…) | BodyPublishers.ofString("…") | .header(…) |
Redirects differ by language: curl follows them only with -L. fetch, requests and Go follow by default; PHP cURL and Java HttpClient do not, so the code turns following on only when the command has -L.
Why converted code behaves differently
Most differences come from the environment, not from the conversion.
Browser-only headers
A command copied from Chrome carries Cookie, User-Agent, sec-ch-ua and similar headers. Browsers refuse to set some of them from fetch, and servers may reject a stale session cookie. Keep only what the API needs.
Tokens pasted into code
Bearer tokens, API keys and session cookies end up in source control easily. Read them from environment variables, and rotate any key that was shared.
CORS in the browser
curl ignores CORS; a browser does not. If the API does not allow your site’s origin, the fetch version fails where curl worked. Call it from your server instead.
Shell variables and files
$TOKEN and @body.json are expanded by the shell or by curl. The code keeps $TOKEN as text and reads @file where it can; replace them with real values.
Send server-side events with one HTTP request
Orders, refunds and anything that never happens in a browser can be sent to TapCub from your backend, as in the sample command.
Server API
Send named events from the backend with a Server Key, into the same project as your web and app data.
Learn moreData-quality checks
Unexpected event names, missing properties and wrong types are flagged; duplicates can be merged or renamed.
Learn moreCustom events and properties
Track orders, sign-ups or any business action with the fields you care about, then chart and filter by them.
Learn morecURL converter questions
Still have a question?
Chat with the team behind TapCub — we usually reply within a few hours on business days.
How do I convert curl to Python requests?
Paste the curl command above. The code uses requests.post, requests.get and so on, with the headers in a dict and the body in json=, data= or files= depending on what curl sends. Install the library with pip install requests.
When does the code use json= and when data=?
If the Content-Type is JSON (or the command uses --json) and the body parses as JSON, the body becomes a Python dict passed as json=, and requests sets the Content-Type itself. Form fields become a dict in data=; any other body is passed as a string in data=.
How are -u, -b and -k converted?
-u user:pass becomes auth=('user', 'pass'), cookies from -b become a cookies= dict, and -k becomes verify=False, which you should remove outside testing.
Why does Python follow a redirect that curl did not?
requests follows redirects by default for every method except HEAD; curl follows them only with -L. Add allow_redirects=False to the call to get curl’s behaviour.
How is a file upload with -F converted?
-F "file=@photo.jpg;type=image/jpeg" becomes an entry in files= with the file name, an open file handle and the content type; plain -F name=value fields go in the same dict as (None, value), which is how requests sends text parts.
Update history
New features, fixes and data refreshes for this tool, newest first.
-
· v1.0
LaunchedFirst release.
Missing something or found an error? Tell us — requests that come up often go into the next update. All tool updates
Inspect what the server sends back
Once the request works, check the response: status, headers, redirects and caching.