[b]"How can I convert Python code to curl for API requests?"[/b] or [b]"What's the best way to convert Python code

16 Replies, 851 Views

"Quick question: How to convert Python code to curl for testing?"

Hey folks!

I’ve got some Python scripts making API calls, but I need to test the same requests in curl. Anyone know the *easiest* way to convert python code to curl?

Like, I’m using `requests` lib rn, and manually rewriting headers + params into curl is a pain.

Is there a tool or trick to automate this? Or do I gotta do it the hard way?

(Also, if you’ve got fav online converters or CLI tools, lmk! 🙏)

Thanks in advance!

---

OR

---

"What’s the best way to convert Python code to curl commands?"

Sup devs,

Struggling to convert python code to curl for some quick API testing. My scripts use `requests.post()` with auth + JSON data, and typing out the curl equivalent feels tedious.

Is there a *smooth* method or tool for this?

I’ve seen some libs that *claim* to do it, but idk if they’re reliable.

Pls share your hacks—saving me from copy-pasting headers one-by-one would be a lifesaver!

Cheers! � (that’s a pineapple, btw)
Yo! If you're using the `requests` library, check out `curlify` (pip install curlify). It’s a lifesaver for converting python code to curl.

Just wrap your request with `curlify.to_curl()` and BOOM—you’ve got a curl command. Works with headers, auth, everything.

Example:
```python
import curlify
import requests

resp = requests.get("https://api.example.com")
print(curlify.to_curl(resp.request))
```

Super clean, no manual work.
Hey! I’ve been there—rewriting Python requests as curl is a pain.

Try Postman’s "Code" feature.

1. Paste your Python code in Postman.
2. Click "Code" (bottom left) and switch to "cURL".

Not fully automated, but way faster than doing it by hand. Plus, Postman’s great for testing anyway.

For CLI folks, `httpie` (https://httpie.io/) also has a `--print=H` flag that spits out curl-like commands.
If you’re lazy like me, just use `requests` debug mode to *almost* get curl commands.

Run your script with:
```bash
HTTPIE_DEBUG=1 python your_script.py
```

It won’t give *perfect* curl, but you’ll see the raw HTTP request. From there, tweaking it into curl is trivial.

Also, +1 for `curlify`—it’s the closest thing to magic for convert python code to curl.
Pro tip: If you’re on Linux/Mac, `mitmproxy` can auto-generate curl commands for *any* HTTP traffic, including your Python scripts.

1. Run `mitmproxy`.
2. Set your Python script’s proxy to `localhost:8080`.
3. Right-click requests in mitmproxy → "Copy as cURL".

Works for *any* lib, not just `requests`.

Downside? Setup takes 2 mins. Upside? Zero code changes.
Wow, didn’t expect so many options!

Tried `curlify` and it’s *exactly* what I needed—saved me so much time.

Postman’s trick is neat too for one-offs.

Follow-up Q: Anyone know if `curlify` handles websockets? Got some WS code to test next...

(Thanks y’all! 🍍)
Not a tool, but a hack:

If your Python code uses `requests`, print `response.request` and manually map it to curl.

Example:
```python
r = requests.get(url, headers=headers)
print(r.request.method, r.request.url, r.request.headers, r.request.body)
```

Then build the curl from those parts. Tedious, but no extra deps.

For quick one-offs, this works. For heavy use, though, tools like `curlify` are worth it.
For those who love CLI tools, `reqsh` (https://github.com/curlpipe/reqsh) can convert python code to curl (and vice versa).

It’s niche but powerful. Supports auth, multipart, everything.

Install via:
```bash
pip install reqsh
```

Then just pipe your Python request code into it.

Fair warning: Docs are sparse, but it *does* work.
If you’re using `httpx` instead of `requests`, it has a built-in `.curl()` method!

```python
import httpx

r = httpx.get("https://api.example.com")
print(r.curl())
```

No extra libs needed.

For `requests`, though, you’re stuck with `curlify` or manual conversion.

---



Users browsing this thread: 1 Guest(s)