cURL to Python

Convert a cURL command into a Python requests call.

cURL Command
Python (requests) Output
Python (requests) code appears here

Related Tools

Documentation

What is curl-to-Python?

This tool converts a curl command into a Python script using the requests library — the de facto standard HTTP client for Python, whose headers, data, auth, and files parameters map almost one-to-one onto curl's own flags.

How it works

The parser walks the curl command's tokens — respecting quoted strings — and builds a structured description of the request: method, url, headers, a raw data string, form fields split into file vs. non-file, an optional Basic Auth pair, and the insecure flag. The Python emitter turns that into a single requests.request(...) call. Headers become a headers dict; -u user:pass becomes auth=(user, pass), which requests turns into an HTTP Basic Authorization header automatically. Critically, a -d/--data body is assigned to a Python string and passed as data=data rather than being json.loads'd into a dict and passed as json=... — that keeps the exact bytes curl would send (key order, whitespace, number formatting) instead of risking requests re-serializing the payload differently.

Features

  • Targets the requests library — one pip install requests away from running
  • Body passed through as a raw string, never re-parsed as JSON
  • -F/--form fields split into a data dict (text fields) and a files dict using open(path, 'rb') (file fields)
  • -u Basic Auth mapped to auth=(user, pass)
  • -k/--insecure mapped to verify=False

Example

Input: curl -X POST https://api.example.com/v1/users -H "Content-Type: application/json" -H "X-Api-Key: abc123" -d '{"name":"Ada Lovelace","role":"admin"}'

Output:

import requests

headers = {
    'Content-Type': 'application/json',
    'X-Api-Key': 'abc123',
}

data = '{"name":"Ada Lovelace","role":"admin"}'

response = requests.request(
    'POST',
    'https://api.example.com/v1/users',
    headers=headers,
    data=data,
)

print(response.status_code)
print(response.text)

Common errors

Because data is a raw string, requests sends it exactly as-is but does not automatically set a Content-Type header for you the way json=... would — if the original curl command didn't include a -H "Content-Type: application/json", the server may not correctly interpret a JSON-looking body. response.status_code being 200 doesn't guarantee success at the application level — check the response body for API-specific error fields.

Best practices

Call response.raise_for_status() if you want an exception on 4xx/5xx instead of silently printing the body. Reuse a requests.Session() across multiple calls to the same host to get connection pooling and cookie persistence for free, and avoid verify=False outside local development against a self-signed certificate.

Frequently Asked Questions

Which HTTP library does the generated code use?

The popular requests library, since it's the de facto standard for HTTP in Python and its API maps directly onto curl's flags (headers, data, auth, files).

Is the request body reconstructed or passed through as-is?

Passed through as the raw string exactly as curl would send it, rather than parsed and rebuilt as a Python dict — this avoids subtly changing the payload (key order, number formatting, whitespace) and matches what curl itself actually sends.

Does it handle -F / --form (multipart) uploads?

Yes — non-file fields become a data dict and file fields (values starting with @) become a files dict using open(path, "rb"), combined in one requests.request() call.

What about -k / --insecure?

Translated to verify=False on the request. Note this disables TLS certificate verification just like curl -k does — only use it against endpoints you trust (e.g. local dev with a self-signed cert).