Skip to content

Curl to Python requests: every common flag and its requests argument

A lookup table for headers, bodies, auth, uploads and redirects, plus the cleanups a converted script needs.

By ClaudeGPT Si Newsroom, Editorial team3 min read

Every curl flag has a matching argument in Python's requests library, so curl to Python requests is mostly a lookup: the URL becomes the first argument, -H becomes headers, -d becomes data, and -X picks the function. The quickest way to do the lookup is to let the free AyeTab cURL converter write the first draft, then adjust the rest by hand with the table below.

The mapping, flag by flag

curl requests Note
curl URL requests.get(URL) GET is the default in both
-X POST requests.post(...) Same for put, patch, delete
-H "Key: value" headers={"Key": "value"} One dict entry per header
-d 'a=1&b=2' data=... Sent as the request body
--json '{...}' json={...} Pass a dict and requests encodes it
-u user:pass auth=("user", "pass") Basic auth
-F file=@a.png files={"file": open("a.png", "rb")} Multipart upload
-b "k=v" cookies={"k": "v"} Request cookies
-L nothing to add requests follows redirects by default
-k verify=False Skips TLS checks; avoid outside local testing
-G -d q=x params={"q": "x"} Query string instead of body

Let a converter do the first pass

Typing that table out for a long command is tedious. AyeTab handles the parts that show up in almost every API example: the URL, the -X method, every -H header and a -d, --data or --data-raw body. Paste the command, set the toggle to Python, and copy the result. It joins commands that are split over several lines with backslashes, and it does the work in your browser tab, so nothing you paste goes to AyeTab's servers.

What you get back is plain requests code ending in print(response.text). Treat it as a starting point and apply the cleanups below.

Three cleanups after converting

Switch JSON bodies to json=. A converter keeps the body as a string passed to data. If the body is JSON, replacing it with a Python dict passed to json= is easier to edit, and requests sets the content type for you.

Move secrets out of the code. Commands copied from docs or dev tools often hold a bearer token or a session cookie. Read it from an environment variable with os.environ instead of pasting the literal value into a file you might commit.

Add a timeout and a status check. requests has no timeout unless you set one, so a hung server can block a script forever. Add timeout=10 and call response.raise_for_status() before using the reply.

Where AyeTab stops

The AyeTab converter is built for the common shape of header plus body. Uploads with -F, basic auth with -u and query flags such as -G are left for you to add using the table. If you convert commands like that often, curlconverter, an open source tool, parses many more curl options and writes code for many more languages, and it is the better choice for that job.

For quick conversions during the day, AyeTab is the pick, mainly because it is already open: the AyeTab extension for Chrome puts the converter, a JSON formatter and the other tools on every new tab and in a side panel, and Ctrl+K jumps straight to any of them.

A note for monitoring scripts

Many people convert curl to requests to schedule a check on an endpoint: a health URL, a price API, a status page. Once the request works, wrap it in a function, log response.status_code and the time taken, and run it from cron or a task scheduler. Keep the timeout short so one slow response does not stall the next run.

FAQ

How do I send JSON with requests instead of a string?

Pass a dict to the json argument. requests encodes it and sets the Content-Type header to application/json for you.

What is the requests version of curl -L?

You do not need one. requests follows redirects by default for every method except HEAD.

Does requests have a default timeout?

No. If you do not pass timeout, a request can wait indefinitely, so add one to every call in a script.

Originally published on claudegpt.si