Convert cURL to Python requests: json=, data=, params= and auth=
Last updated: · 8 min read
You try an API in the terminal with curl first and then move the same request into a Python script. In that move every curl option lands in a different argument of the requests library: headers go into the headers dictionary, query parameters into params, a JSON body into json=, form data into data=, -u into an auth tuple and -F into files. A wrong mapping rarely raises an error; it just sends the server a different request. This guide explains which option the cURL to Python Converter turns into which argument, where the defaults of curl and requests part ways, and what to check before you use the output. The outputs in the examples are the tool's real results.
Formula
Rules:
1. The command is split into words the way a shell does it (single and double quotes, $'…', a trailing \ for line continuation).
2. If the method is GET, POST, PUT, PATCH, DELETE, HEAD or OPTIONS in upper case, the call is written as requests.get(…), requests.post(…) and so on; any other method becomes requests.request("METHOD", …).
3. A body that is a valid JSON object or array, sent with a Content-Type containing json, becomes json={…}; true, false and null are written as True, False and None. Any other body stays a data="…" string.
4. Data moved into the URL with -G is written, decoded, as a params={…} dictionary as long as no key repeats.
5. -u becomes auth=("user", "password"), -b becomes cookies={…} and -F becomes files={…}; text fields are written as (None, "value") and file fields with open("path", "rb").
6. -L adds allow_redirects=True and -k adds verify=False.
7. Options the tool does not know are not converted; they are listed in a warning line at the top of the output.
Input 1:
curl -X POST https://api.example.com/users \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_TOKEN" \
-d '{"name": "Ada Lovelace", "role": "admin"}'
Output 1:
import requests
response = requests.post(
"https://api.example.com/users",
headers={
"Content-Type": "application/json",
"Authorization": "Bearer YOUR_TOKEN",
},
json={
"name": "Ada Lovelace",
"role": "admin",
},
)
print(response.text)
# Supported curl options: -X, -H, -d, --data-raw, --data-binary, --data-urlencode, --json, -F, --form-string, -u, -b, -A, -e, -G, -I, -L, -k, --url. Options that do not change the request (-o, -s, -v, -i, --compressed…) are skipped; any others are listed in a warning.
Input 2:
curl -G "https://api.example.com/search" --data-urlencode "q=café latte" -d "page=2" -u "demo:s3cret" -L -k --max-time 10
Output 2 (the closing note is the same as in Output 1 and is not repeated here):
# Warning: unsupported curl options were ignored: --max-time
import requests
response = requests.get(
"https://api.example.com/search",
params={
"q": "café latte",
"page": "2",
},
auth=("demo", "s3cret"),
allow_redirects=True,
verify=False,
)
print(response.text)
Input 3:
curl https://api.example.com/upload -F "file=@report.pdf;type=application/pdf" -F "title=Q3 report" -b "session=abc123; theme=dark"
Output 3:
import requests
response = requests.post(
"https://api.example.com/upload",
cookies={
"session": "abc123",
"theme": "dark",
},
files={
"file": ("report.pdf", open("report.pdf", "rb"), "application/pdf"),
"title": (None, "Q3 report"),
},
)
print(response.text)There is no formula here, only a set of mapping rules. According to curl's manual, -d sends the data with POST and the content type application/x-www-form-urlencoded; according to the Requests documentation, a string passed as data= is posted as it is, while an object passed as json= is encoded as JSON and the Content-Type header is set to application/json. The tool only produces text: it does not run the generated Python, and the developers have not tested the output by executing it in a Python interpreter either. Read the code and try it in your own environment before relying on it.
What the inputs mean
- cURL command
- A single command that starts with curl. Commands split with a trailing \, a leading $ prompt and the spelling curl.exe are accepted. Up to 1,000,000 characters are processed; if the command does not start with curl, the tool returns an error.
Worked example: A JSON POST, a search with parameters and a file upload
- In Input 1, -X POST selects the requests.post( call. The two -H options go into the headers dictionary.
- The body is a valid JSON object and the Content-Type is application/json, so it is written as json={ "name": "Ada Lovelace", "role": "admin" }. requests sets the Content-Type header itself when json= is used; the line in headers is a harmless duplicate.
- Input 2 uses -G. --data-urlencode "q=café latte" and -d "page=2" go into the query string, not the body; the tool writes them decoded as params={ "q": "café latte", "page": "2" } and leaves the percent-encoding to requests.
- -u "demo:s3cret" becomes the tuple auth=("demo", "s3cret"); requests builds the Authorization: Basic header.
- -L adds allow_redirects=True and -k adds verify=False. --max-time is not converted and is reported on the first line of the output: Warning: unsupported curl options were ignored: --max-time
- In Input 3, -b "session=abc123; theme=dark" becomes a cookies dictionary with two keys. The field -F "file=@report.pdf;type=application/pdf" is written as "file": ("report.pdf", open("report.pdf", "rb"), "application/pdf") and -F "title=Q3 report" as "title": (None, "Q3 report").
Result: The three commands become, in turn, a requests.post call with a json= body, a requests.get call with params= and auth=, and a requests.post call with cookies= and files=. Every output ends with print(response.text).
🐍 cURL to Python Converter (requests)
Free, no sign-up; your data stays in your browser.
Open the calculator →How to read the result
The output carries the shape of the request over correctly; the behavioural differences are yours to close. Redirects: curl follows them only with -L, whereas requests follows them by default for every method except HEAD. The tool writes allow_redirects=True only when -L is present, so add allow_redirects=False if you want curl's behaviour without -L. Timeouts: requests has no default timeout, and because --max-time is not converted you should pass timeout= yourself. Body bytes: json= re-serialises the object, so whitespace and formatting may not match the original command; for requests that are signed over the body, send the original string with data=. verify=False switches certificate verification off and leaves the connection open to man-in-the-middle attacks; keep it to local experiments. In uploads, the file opened with open() is never closed; use a with block in code you intend to keep. On security: the conversion runs in your browser and the command is not sent anywhere. Even so, replace real keys, cookies and passwords with placeholders before pasting a command into any website, and in the script read them from environment variables instead of writing them into the code. This is general information.
Common mistakes
- Running the generated code without reading it. The output has not been tested in a Python interpreter; go over the URLs, headers and body.
- Expecting $TOKEN or $(…) to be resolved. The tool does not expand shell variables; the value "Authorization": "Bearer $TOKEN" reaches the code unchanged. In Python, read it with os.environ["TOKEN"].
- Assuming a body read from a file with -d @payload.json will appear in the code. The tool cannot access the file, writes a call without a body and warns: Warning: -d reads from a file (payload.json); its contents cannot be included here, add them yourself.
- Leaving a URL that contains & unquoted. The shell cuts the command at &; the tool converts only the first part and reports: Warning: the command was cut at the unquoted &; the shell treats what follows as a separate command. If the character belongs to the URL, wrap the URL in quotes.
- Sending JSON with -d but without a Content-Type. curl posts it as form data, and the tool likewise writes a data="…" string with an application/x-www-form-urlencoded header. Add the header or use --json.
- Treating response.text as parsed JSON. The tool always writes print(response.text); use response.json() for a JSON reply and response.raise_for_status() to catch error statuses.
- Pasting a different command such as wget or http. The tool only parses curl commands: The command must start with curl: use the form curl [options] <url>.
Frequently asked questions
What is the difference between json= and data= in requests?
json= takes a Python object, encodes it as JSON text and sets the Content-Type header to application/json. data= form-encodes a dictionary, or sends a string as it is, and does not set a JSON content type. The tool produces json= when the body is valid JSON and the header contains json, and data= otherwise.
What is the Python equivalent of curl -u?
The tuple auth=("user", "password"); requests sends it as HTTP Basic authentication. If the command also contains an Authorization header, the tool does not add auth= and leaves the header in headers.
Is verify=False a safe replacement for curl -k?
No. The Requests documentation states that with verify=False any certificate presented by the server is accepted and the application becomes vulnerable to man-in-the-middle attacks. The lasting fix is a valid certificate, or pointing verify= at your own CA bundle.
How do I write a curl -F file upload in requests?
With the files= dictionary. A file field is written with open("path", "rb"), or as a (name, file, type) tuple when a file name or type is given; text fields in the same form become (None, "value"). Do not set the Content-Type header yourself in that case; requests adds the boundary.
Why are the parameters from curl -G decoded in the params dictionary?
requests encodes the values in params itself when it builds the URL. That is why the tool writes "café latte" rather than caf%C3%A9+latte. If the same key occurs more than once a dictionary cannot hold it, and the query string is appended to the URL directly.
Is my command sent to a server?
No, the conversion happens in your browser. The safest habit is still never to paste real keys and passwords into an online tool.
The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.