gizmobench
BrowseDaily

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.
Python requests
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)
converted as text: nothing is run, read from disk or sent
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

  • -H 'X-Request-Label: weekly report draft'a quoted header with spaces
    one header, X-Request-Label: weekly report draft
  • -X PUT with -d 'qty=3'an explicit method and data
    method PUT, body qty=3; without -X the same -d would be POST
  • $(cat token.txt), a backtick, or -d @body.jsonshell interpolation or a file reference
    3 problems flagged and not converted: nothing is run or read

What each flag becomes

curl flagIn the code
-X, --requestthe method, as written
-H, --headera header; 'Name;' sends it with an empty value
-d, --data, --data-asciithe body and POST, with curl's form Content-Type when none is set; @file is refused
--data-rawthe body, with a leading @ sent as written
--data-binarythe body as written; @file is refused
--data-urlencodea percent-encoded part; name=value encodes only the value
--jsonthe body, with JSON Content-Type and Accept headers
-F, --form-stringtext fields of a multipart form; file parts are refused
-G, --getthe data moved into the URL's query
-I, --heada HEAD request
-u, --userBasic auth: a header in JavaScript, and auth= in requests when the name and password are ASCII
--oauth2-beareran Authorization: Bearer header
-A, -e, -bUser-Agent, Referer and Cookie headers; -b with a file name is refused
-m, --max-timea timeout in each library's own terms
-L, --locationredirects followed, the libraries' default, with allow_redirects=True for a HEAD request in requests
-k, --insecureverify=False in requests, an https.Agent in Axios; an error for Fetch
--compressednothing extra: the libraries ask for compressed responses by default
-s, -S, -v, -i, -o, -w, --faillisted as ignored: they change only curl's own output
-x, --proxy and any other flagan error that names the flag
Accuracy. 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.

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.