Skip to content

curl to Python Converter

Paste a curl command and get Python code using the requests library: headers, JSON bodies, form data, file uploads, basic auth, timeouts and more.
import requests

url = "https://jsonplaceholder.typicode.com/posts"
headers = {
    "Content-Type": "application/json",
    "Authorization": "Bearer YOUR_TOKEN",
}
json_data = {
    "title": "Hello",
    "body": "From curl",
    "userId": 1,
}

response = requests.post(
    url,
    headers=headers,
    json=json_data,
)
print(response.json())

Converted in your browser. Commands often contain API keys, so nothing is sent anywhere or saved.

How to convert curl to Python

  1. Paste your curl command (from API docs, a terminal or your browser's dev tools).
  2. The Python code appears instantly, using requests. Switch tabs to see other languages.
  3. Copy the code and run it after installing the library: pip install requests.

How curl options map to requests

curlPython
-X POSTrequests.post(...)
-H 'Name: value'headers={...}
-d '{"json": true}'json={...} (a Python dict)
-d 'a=1&b=2'data={...}
-F 'file=@photo.jpg'files={"file": open("photo.jpg", "rb")}
-u user:passauth=("user", "pass")
-m 10timeout=10
-kverify=False

Tips for the generated Python code

  • Always pass timeout=. requests waits forever by default, so one slow API can hang your script.
  • Call response.raise_for_status() to turn 4xx and 5xx responses into exceptions instead of silently using an error body.
  • Making many calls to the same API? Use a requests.Session() to reuse connections and shared headers.

Convert curl to other languages

The same converter writes JavaScript, Node.js (axios), Go, PHP and Ruby. See the cURL converter for every supported option.

Need an API to try it on? Every API in our list of APIs with no key works with a plain curl command.

Frequently asked questions

Should I use json= or data= in Python requests?

Use json= to send a JSON body: requests serialises the dict and sets Content-Type: application/json for you. Use data= for form-encoded bodies (like curl -d 'a=1&b=2') or a raw string. The converter picks the right one from the curl command's Content-Type.

Does requests follow redirects like curl -L?

Yes. requests follows redirects by default (except for HEAD requests), so curl -L needs nothing extra. To match curl without -L, pass allow_redirects=False.

Can I use the output with httpx instead of requests?

Mostly, yes: httpx.post(url, headers=..., json=..., data=..., files=..., auth=...) takes the same arguments. Two differences: httpx doesn't follow redirects unless you pass follow_redirects=True, and its default timeout is 5 seconds.

How do I copy a request as cURL from Chrome?

Open DevTools (F12), go to the Network tab and reload. Right-click the request, choose Copy, then Copy as cURL (bash). Paste it here. Chrome's Windows (cmd) format works too.

Is it safe to paste commands that contain API keys?

Yes. The conversion runs entirely in your browser; nothing you paste is sent to a server, logged or saved. Still, rotate any key you've shared publicly.

More free developer tools