How to Convert cURL Commands to Python
A cURL command copied from documentation is a useful reference when building a Python integration. Translate the HTTP request it represents, including method, URL, body and redirects. Treat shell behavior and local files as separate concerns.
Port a small JSON request
This command posts a JSON object. In Python, requests accepts the corresponding dictionary through json= and serializes it with a JSON content type. Keep the example on a placeholder host when documenting the conversion. Adapt the endpoint inside your own test environment.
Use an explicit timeout and handle unsuccessful responses. In this example the redirect option is disabled to match cURL’s usual behavior without -L. In an integration, decide whether following a redirect is expected, especially when a request can change state.
curl 'https://example.com/api/items' --json '{"name":"demo"}'
import requests
response = requests.post(
"https://example.com/api/items",
json={"name": "demo"},
timeout=10,
allow_redirects=False,
)
response.raise_for_status()Choose json, data or files based on the original request
A JSON dictionary fits json=. A literal body or form fields belong in data=, with headers chosen to match the original content type. A multipart upload uses files= and needs an explicit file read in Python. Do a byte-level comparison when a test depends on the exact body.
A command containing shell variables, command substitutions or @file depends on the environment that executes it. The browser converter accepts literal arguments. Replace environment-dependent pieces with synthetic examples while you convert, then load them explicitly in the Python script.
Troubleshoot a request that changes behavior
Compare the request method and destination first. Then check the body format, content type and redirect settings. A successful connection with a 400 response often points to a contract mismatch. The response body may identify the field that failed validation.
Commands copied from the Network panel may include session cookies, bearer tokens and one-time headers. Redact those values from examples and use scoped test credentials through your normal secret management workflow. Keep output containing raw credentials private.
Frequently asked questions
- Does the converter send the request?
- It generates code in the browser. Executing the Python snippet sends the request.
- What if the server responds with HTML?
- Check status, content type and redirect history before calling response.json(). An authentication gateway may return an HTML page.