ITInfoByte

How to convert a curl command to Python requests

By Amirtham ·

Open curl to Code Converter →

API documentation usually shows examples as curl commands, and browser DevTools can copy any request as one (Network tab → right-click a request → Copy → Copy as cURL). Turning that into Python by hand is mostly mechanical: method, URL, headers and body each map onto an argument of requests.

A worked example

This command creates an order through a JSON API:

curl -X POST https://api.example.com/v1/orders \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer TOKEN" \
  -d '{"sku": "A-100", "qty": 2}'

Pasted into the curl converter with Python selected, it becomes:

import requests

headers = {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer TOKEN'
}

payload = {
    "sku": "A-100",
    "qty": 2
}

response = requests.post('https://api.example.com/v1/orders', headers=headers, json=payload)
response.raise_for_status()
print(response.json())

Because the body is valid JSON, it is passed with json=, which serialises the dictionary for you. A non-JSON body (form data, plain text) is passed with data= instead. raise_for_status() turns 4xx/5xx responses into exceptions so failures aren't silently ignored.

How curl flags map to requests

  • -X POST → requests.post(...) (curl also implies POST when -d is present)
  • -H "Name: value" → an entry in headers
  • -d, --data, --data-raw → json= for JSON, otherwise data=
  • -u user:pass → auth=('user', 'pass')
  • -F field=value → multipart form fields

Flags for proxies, client certificates, --resolve and retries are ignored rather than guessed, so check those by hand if your command uses them.

Before you paste a copied request

"Copy as cURL" includes your cookies and authorisation headers — effectively your logged-in session. The converter parses the command inside your browser and sends it nowhere, but remove secrets before sharing the generated code with anyone or committing it. In real code, read tokens from environment variables instead of hard-coding them.

Questions

Can it convert curl to JavaScript or Go as well?

Yes — the same tool outputs JavaScript fetch, Node.js axios and Go net/http.

Why is my multi-line command not parsed?

Lines must be joined with a trailing backslash, as in a Unix shell. Windows cmd's ^ continuations should be converted first.

Tools used in this guide