Free tool · Developer tools
curl to Python
Paste a curl command and copy the same request as Python code that uses the requests library. Flags the tool cannot convert are listed for you, never dropped quietly.
- Free
- No sign-up
- Runs in your browser
POST requesthttps://api.example.com/v1/orders
- Every flag was converted or explained
- 1 note
- Without -L curl does not follow redirects, so the code sets allow_redirects=False. requests follows them by default.
- Method
- POST
- Headers
- 2
- Body
- JSON
- Login
- none
How to use the cURL to Python Converter
- Paste the command into Your curl command. Browsers can copy a request as curl: choose the bash format.
- Tick or untick Use json= when the body is JSON and Add lines that print the answer.
- Read the code, then press Copy or Download .py.
- Read Needs your attention and Notes under the code. They list every flag that was not converted and why.
How do I convert a curl command to Python?
Paste the command and the code appears. The example is a POST to https://api.example.com/v1/orders with 2 headers and a JSON body. It becomes a requests.post( call. The headers sit in a headers dictionary, and the body is a Python dictionary passed as json=. Install the library once with pip install requests, then run the file.
The tool reads the command the way a shell does. Text in single quotes is taken as written, text in double quotes allows a backslash before a quote, and a backslash at the end of a line joins it to the next. That is why a long command copied across several lines still works. A command that does not start with the word curl, or that has no address, gets a plain message instead of code.
The method comes from -X when it is given. Without it, -I means HEAD, a body means POST, and anything else means GET. Replace the sample token with your own before you run the code, and keep real keys out of files you share.
Which curl flags does it convert?
The table runs the tool on one small command per flag and shows the Python line that results. The address is shortened to URL in the second column.
| Flag | curl command | Python line |
|---|---|---|
-X PUT | curl -X PUT URL | response = requests.put( |
-H | curl URL -H 'Accept: application/json' | 'Accept': 'application/json' |
-d | curl URL -d 'a=1&b=2' | data = 'a=1&b=2' |
--json | curl URL --json '{"a":1}' | json=json_data |
-u | curl URL -u ann:pw | auth=('ann', 'pw') |
-b | curl URL -b 'sid=abc' | 'sid': 'abc' |
-k | curl URL -k | verify=False |
-m | curl URL -m 15 | timeout=15 |
-L | curl URL -L | response = requests.get( |
-F | curl URL -F name=Ann | 'name': (None, 'Ann') |
-G with -d | curl -G URL -d q=tea | params='q=tea' |
-I | curl -I URL | response = requests.head( |
Short flags can be joined, so -sSL is read as three flags, and a value can follow a flag directly, as in -XPOST. Several -d values are joined with an ampersand, which is what curl does. --data-urlencode encodes the value for you, and --data-raw keeps a leading @ as text instead of reading a file.
What happens to flags it does not understand?
Nothing is dropped without a word. Each flag ends up in one of two lists. Needs your attention holds flags that the tool could not convert and that may change the request, such as an unknown option or an upload with -T. Notes holds flags that were left out on purpose, with the reason, such as -s, which only controls curl's own output.
For the command curl https://example.com/api -s --compressed --frobnicate -o out.txt the tool lists 1 warning and 4 notes. The warning reads: --frobnicate is not understood by this tool and was left out. The notes explain that --compressed is not needed because requests already unpacks gzip, and that -o saves to a file, which you do in Python by writing the content out.
A shell variable such as $TOKEN is also flagged. Python cannot read it from the command, so the text is kept as written and you replace it. A pipe or a redirect ends the command, with a note. The Windows cmd style, which uses a caret at the end of a line, is refused with a message, because its escapes differ: copy the request again in bash format.
How are JSON, form data and file uploads converted?
A JSON body becomes the json= argument only when the request says it is JSON: with --json or a Content-Type: application/json header. Without that header curl sends the body as a form, so the tool keeps it as text to stay faithful. The requests documentation says the json argument is ignored when data or files is also given, and the tool never writes both.
The JSON is read by the browser and written back as a Python dictionary, so spaces and key order inside the body may differ from the original. Numbers are a risk: a number with more than 15 digits would change. For {"id":12345678901234567890} the tool keeps the body as text, and the code starts data = '{"id":12345678901234567890}'. Untick the JSON option if you want every body kept as text.
Form uploads with -F become a files dictionary. A plain field is written as (None, value) and a file as a name, an open file and an optional type. Open files are read from where the Python runs, so the path must exist there. If the same field name appears twice, a list is used, since a dictionary keeps one.
Why does the code set allow_redirects and timeout?
curl and requests differ here. curl does not follow a redirect unless you pass -L. The requests documentation says it follows redirects by default for every method except HEAD. To keep the behavior the same, the tool writes allow_redirects=False when -L is missing, as in allow_redirects=False,, and writes nothing when -L is given.
Timeouts differ too. The same page says that without a timeout, requests wait without limit. The tool copies -m and --connect-timeout into timeout=, and adds none otherwise. Production code should set one. The -k flag becomes verify=False, which turns off certificate checks, so use it only on a server you trust. Your command is converted in the browser and is not uploaded.
Use it with
- JSON Formatter: format and validate JSON.
- Base64 Encoder Decoder: encode or decode Base64.
- JSON to TypeScript: typeScript interfaces from a JSON sample.
Questions
How do I convert curl to Python requests?
Paste the command and copy the code. The example POST becomes requests.post with a headers dictionary and json=.
Does it convert JSON bodies?
Yes, when the command says the body is JSON (--json or a Content-Type: application/json header). Otherwise the body stays a text string, as curl would send it.
What happens to flags it does not know?
They are listed under Needs your attention with the flag name. Flags left out on purpose, such as -s or --compressed, are listed under Notes with the reason.
Why is allow_redirects=False in my code?
curl does not follow redirects without -L, but requests follows them by default. The code sets allow_redirects=False to match curl.
How do I upload a file?
Use -F field=@path in curl. The code uses a files dictionary with open(path, 'rb'), and the file must exist where the Python runs.
Is my command uploaded anywhere?
No. It is read by code in your browser and is not sent or stored. Remove real tokens before you share the result.
The tool's results are computed in your browser by the code on this page, using the sources listed above; the explanations are checked against the tool's own output.