cURL to Python requests
This page opens Python requests output before reading your curl command. Paste a bash-style command and inspect import requests, the URL, headers and data or files parameters. Inline JSON POST bodies and supported Basic authentication are carried into the source. Conversion is local text processing: it does not run curl, install requests or make the HTTP call. Read the notes for redirect, timeout and other client-library differences.
method POST, set by -d url https://api.example.com/v1/notes headers 2 Content-Type: application/json X-Request-Label: weekly report draft body 38 bytes errors none: no unsupported flags notes - Without -L curl stops at a redirect, and this code follows it, as requests does by default.
import requests
url = "https://api.example.com/v1/notes"
headers = {
"Content-Type": "application/json",
"X-Request-Label": "weekly report draft",
}
data = '{"title":"Weekly report","done":false}'
response = requests.post(url, headers=headers, data=data)- Method
- POST, from -d
- Headers
- 2
- Body
- 38 bytes
- Code
- Python requests
The command is converted in this tab. It is never saved, sent or added to analytics, and that matters because a pasted command often carries an Authorization header, a -u password or a session cookie. The page remembers only the language you picked, and Start over above the tool forgets it.
Examples
- one header, X-Request-Label: weekly report draft
- method PUT, body qty=3; without -X the same -d would be POST
- 3 problems flagged and not converted: nothing is run or read
What each flag becomes
| curl flag | In the code |
|---|---|
| -X, --request | the method, as written |
| -H, --header | a header; 'Name;' sends it with an empty value |
| -d, --data, --data-ascii | the body and POST, with curl's form Content-Type when none is set; @file is refused |
| --data-raw | the body, with a leading @ sent as written |
| --data-binary | the body as written; @file is refused |
| --data-urlencode | a percent-encoded part; name=value encodes only the value |
| --json | the body, with JSON Content-Type and Accept headers |
| -F, --form-string | text fields of a multipart form; file parts are refused |
| -G, --get | the data moved into the URL's query |
| -I, --head | a HEAD request |
| -u, --user | Basic auth: a header in JavaScript, and auth= in requests when the name and password are ASCII |
| --oauth2-bearer | an Authorization: Bearer header |
| -A, -e, -b | User-Agent, Referer and Cookie headers; -b with a file name is refused |
| -m, --max-time | a timeout in each library's own terms |
| -L, --location | redirects followed, the libraries' default, with allow_redirects=True for a HEAD request in requests |
| -k, --insecure | verify=False in requests, an https.Agent in Axios; an error for Fetch |
| --compressed | nothing extra: the libraries ask for compressed responses by default |
| -s, -S, -v, -i, -o, -w, --fail | listed as ignored: they change only curl's own output |
| -x, --proxy and any other flag | an error that names the flag |
Common questions
- How do I convert a curl POST with JSON to Python?
- Paste the command with inline --json, --data or --data-raw. Supported body text becomes a data value and the curl Content-Type becomes a header. The source uses requests.post unless -X supplies another method. File-backed bodies such as -d @body.json are refused; paste the actual body instead.
- How are credentials and non-ASCII bodies handled?
- Plain ASCII -u user:password can become the requests auth tuple. Headers and bodies containing non-ASCII text are encoded as UTF-8 where necessary. The generated file carries any credentials you supplied, but the converter does not store the command or send it.
- Is Python requests equivalent to every curl option?
- No. Unsupported flags are errors. Notes explain differences such as redirect defaults and requests timeout applying to each wait for data rather than curl -m limiting the complete transfer. The generator supports requests, not aiohttp, pycurl or a subprocess wrapper around curl.
- What is the Python equivalent of curl in this tool?
- A requests method call with the supported request settings. The output begins with import requests. Review and use that source in a Python environment where requests is available; nothing is installed or executed here.
Converts the text of one curl command, up to 64 KB, and never runs shell interpolation, reads a local file or sends the request, so $( ), backticks, variables and @file references are errors. Flags it does not convert are named as errors too, apart from flags that change only curl's own output, such as -s, -o and --fail, which are listed as ignored. The code carries the method, URL, body and every header the command sets, with -d meaning POST unless -X names another method, escaped for the chosen language.