curly{bits}

curlybits / cURL Converter / cURL to Python

cURL to Python Converter

Paste a curl command and get clean Python code that uses the requests library: headers and cookies as dictionaries, JSON bodies as Python dicts with json=, form posts with data=, uploads with files= and query strings split into params=.

cURL command
Code

      

How to use the cURL to Python Converter

  1. Paste a curl command into the left box (from API docs or Copy as cURL in your browser).
  2. Python requests is already selected.
  3. Check the notes under the code for anything that needs your attention, like files to load.
  4. Click Copy, then run it after pip install requests.

How curl options become requests arguments

curlrequests
-H 'Name: value'headers={"Name": "value"}
-d '{"a": 1}' with a JSON content typejson={"a": 1} (requests sets the Content-Type header for you)
-d 'a=1&b=2'data={"a": "1", "b": "2"}
-F '[email protected]'files={"file": ("photo.jpg", open("photo.jpg", "rb"))}
?page=2&q=thai in the URL, or -G -dparams={"page": "2", "q": "thai"}
-u user:passauth=("user", "pass")
-b 'session=abc'cookies={"session": "abc"}
-m 10timeout=10
-kverify=False

Tips for the generated code

  • Always set a timeout in real scripts. requests waits forever by default, which can hang a job.
  • Check errors with response.raise_for_status() before reading response.json().
  • Reuse connections: for many calls to the same API, create a requests.Session() and call session.get(...). It keeps the connection and cookies.
  • Keep secrets out of the code: move tokens to environment variables with os.environ["API_TOKEN"].
  • Async code? httpx has almost the same API: change requests.post to httpx.post, or use httpx.AsyncClient.

Frequently asked questions

Does requests follow redirects like curl -L?

Yes, requests follows redirects by default, while curl only does with -L. Add allow_redirects=False if you need curl's default behaviour.

Why is my -F form sent with files=?

curl -F always sends multipart/form-data. In requests, only files= does that, so plain fields are written as (None, value) pairs.

Does it work with Python 2?

The code uses requests features that work on Python 3.8 and later. Python 2 is no longer supported by requests.

Is my command uploaded?

No, it's converted in your browser.

Updated