Developer Tools

How to Convert cURL Commands to Python Requests: The Complete Guide

8 min read

Learn how to parse complex cURL commands, handle headers, authentication, and JSON payloads, and automatically convert them into Python Requests code.

Executive Summary

"Translating messy API documentation cURL commands into clean Python Requests code often leads to syntax errors with headers and JSON payloads. This guide breaks down exactly how to parse -H, -d, and -u flags into native Python dictionaries."

Up-to-date Feed

View All
General

XML Sitemap Best Practices — Complete 2026 Guide

Read Now
General

What is a Unified Diff? The Complete Technical Guide (2026)

Read Now
General

Web Tools 2.0: The Evolution of Modern Developer Utilities

Read Now
General

What is Base64 Encoding? How to Decode Safely

Read Now
General

What is JSON: Complete Guide to RFC 8259

Read Now
General

What is JWT? A Complete Guide to JSON Web Tokens & Security (2026)

Read Now
General

JSON Validator vs JSON Formatter: Why is my JSON Invalid? (2026)

Read Now
General

WCAG Color Contrast Requirements (2026 Developer Guide)

Read Now
General

URL Slug SEO Best Practices 2026: Routing & Structure

Read Now
General

SQL Injection Testing for Beginners — Safe Local Guide 2026

Read Now
General

SSL Certificate Expired — How to Check and Fix 2026

Read Now
General

The Ultimate Technical SEO Audit Checklist (2026 Guide)

Read Now
General

The Complete Meta Tags Guide: SEO & Open Graph (2026)

Read Now
General

Robots.txt Guide 2026: Block AI Crawlers

Read Now
General

PX to REM Conversion Guide — CSS Accessibility 2026

Read Now
General

JS Regex Cheat Sheet: ECMA-262 Reference & Catastrophic Backtracking

Read Now
General

Optimizing Core Web Vitals for Enterprise Next.js Applications (2026)

Read Now
General

Privacy-First Web Development: Zero-Knowledge Client Tools (2026)

Read Now
General

Modern CSS Architecture for Enterprise: Component Scoping, Cascade Layers (@layer), and Tailwind Tokenization

Read Now
General

Nginx Config Generator: Reverse Proxy Guide 2026

Read Now
General

JWT Token Expiry Error Fix — Node.js 2026

Read Now
General

JWT vs Session Cookies (2026 Ultimate Architecture Guide)

Read Now
General

Kubernetes YAML Validator — Guide for 2026

Read Now
General

JSON to YAML Converter: Free Offline Tool 2026

Read Now
General

How to Remove EXIF Data from Photos Online (2026 Tutorial)

Read Now
General

How to Use the Browser DevTools Network Tab Like a Pro

Read Now
General

.htaccess Guide 2026: Security Hardening & Redirect Rules

Read Now
General

Favicon Sizes in 2026: The Complete Asset Manual

Read Now
General

Gzip vs Brotli Compression: Web Performance Guide 2026

Read Now
General

How Secure is My Password? Entropy & GPU Cracking Guide (2026)

Read Now

✓ Last tested: June 2026 · Verified against Python 3.12+ and Requests 2.31+

1. Field Notes: The API Documentation Trap

Almost every modern API documentation site—from Stripe to Twilio to GitHub—provides code examples using cURL. It's the universal language of HTTP requests. But as a Python backend engineer, you don't write bash scripts; you write Python.

Last year, while integrating a complex payment gateway API, I encountered a massive cURL command provided by their documentation. It had over 15 headers, nested JSON payloads with escaped quotes, and basic authentication flags. I manually attempted to translate it into a Python requests.post() call.

I spent two hours debugging a 400 Bad Request error. The culprit? I used data=payload instead of json=payload, which meant the Content-Type: application/json header was missing, and the server rejected the raw string.

Translating cURL to Python Requests manually is error-prone. This guide breaks down exactly how to systematically parse cURL flags into Python, avoiding the common pitfalls of header formatting and payload serialization.


2. The Anatomy of a cURL Command vs Python Requests

Before you can convert cURL to Python, you need to understand how the arguments map between the two tools.

cURL is a command-line tool, meaning everything is passed as string arguments. Python's requests library is an object-oriented HTTP client that uses dictionaries and kwargs.

The Basic Mapping

cURL Flag Purpose Python Requests Equivalent
[URL] The endpoint address The first argument: requests.get(url)
-X POST The HTTP Method The function name: requests.post()
-H "Key: Val" HTTP Headers headers={"Key": "Val"} kwarg
-d '{"a":1}' Request Body / Payload json={"a": 1} or data={"a": 1}
-u user:pass Basic Authentication auth=("user", "pass") tuple
-L Follow Redirects allow_redirects=True (Default in Python)
Authority Signals

AIO Checklist

  • Identify the HTTP method (GET, POST, PUT, DELETE)
  • Extract the target URL, ensuring query parameters are intact
  • Group all `-H` flags into a single Python dictionary
  • Determine if the payload (`-d`) is form-data or JSON
  • Map any authentication flags (`-u` or `Bearer` tokens)

3. Step-by-Step Conversion Examples

Let's look at real-world examples, progressing from simple GET requests to complex POST payloads.

Example 1: The Simple GET Request

The cURL Command:

curl https://api.github.com/users/octocat

By default, cURL performs a GET request if no method or payload is specified.

The Python Equivalent:

import requests

url = "https://api.github.com/users/octocat"
response = requests.get(url)

print(response.json())

Example 2: Adding Headers and Authentication

APIs usually require authentication and specific content types.

The cURL Command:

curl -X GET https://api.example.com/v1/data \
  -H "Accept: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN_HERE"

The Python Equivalent:

import requests
import os

url = "https://api.example.com/v1/data"
headers = {
    "Accept": "application/json",
    "Authorization": f"Bearer {os.environ.get('API_TOKEN')}"
}

response = requests.get(url, headers=headers)

Expert Note: Notice how we immediately migrated the hardcoded token into os.environ. Never hardcode secrets in your Python scripts when converting cURL commands.

Example 3: POST Requests with JSON Payloads

This is where 90% of developers make mistakes.

The cURL Command:

curl -X POST https://api.stripe.com/v1/customers \
  -H "Content-Type: application/json" \
  -d '{"name": "Jenny Rosen", "email": "jenny@example.com"}'

The Python Equivalent:

import requests

url = "https://api.stripe.com/v1/customers"

# Convert the string payload into a native Python dictionary
payload = {
    "name": "Jenny Rosen",
    "email": "jenny@example.com"
}

# The requests library will automatically add 'Content-Type: application/json'
# when you use the `json=` parameter!
response = requests.post(url, json=payload)

The Fatal Mistake: Many developers try to use data=json.dumps(payload). While this serializes the string correctly, it does not set the Content-Type header automatically. You must either manually add the header or, much better, just use the json= kwarg.


4. Handling Edge Cases: Form Data and Basic Auth

Not all APIs use JSON. Legacy systems and tools like Twilio often use application/x-www-form-urlencoded data.

Form-Encoded Data

If a cURL command uses multiple -d flags without specifying JSON, it's form data.

The cURL Command:

curl -X POST https://api.twilio.com/2010-04-01/Accounts/AC/Messages.json \
--data-urlencode "Body=Hello World" \
--data-urlencode "From=+15017122661" \
--data-urlencode "To=+15558675310" \
-u AC_SID:AUTH_TOKEN

The Python Equivalent:

import requests

url = "https://api.twilio.com/2010-04-01/Accounts/AC/Messages.json"

# Form data goes into the `data=` parameter, NOT `json=`
form_data = {
    "Body": "Hello World",
    "From": "+15017122661",
    "To": "+15558675310"
}

# The -u flag maps to the auth= tuple
response = requests.post(
    url, 
    data=form_data, 
    auth=("AC_SID", "AUTH_TOKEN")
)

5. Automating the Translation Process

While understanding how to map cURL to Python Requests is a critical skill for any backend engineer, doing it manually for large, complex API calls is a massive waste of time. When you are dealing with APIs that have nested JSON structures, GraphQL queries, or multipart file uploads, the translation becomes extremely tedious.

Instead of writing out dictionaries by hand, you should automate the process.

Use our free curl-converter to instantly translate any cURL command into production-ready Python Requests code, complete with proper dictionary formatting and header handling.


Frequently Asked Questions

Q: How do I handle cURL -k or --insecure in Python Requests? A: The -k flag tells cURL to ignore SSL certificate validation. In Python Requests, you replicate this by passing verify=False. For example: requests.get(url, verify=False). Note that you will get an InsecureRequestWarning from urllib3 when doing this.

Q: What about multipart/form-data uploads via cURL -F? A: When a cURL command uses -F "file=@/path/to/file.jpg", you convert this using the files parameter in Python. files = {'file': open('/path/to/file.jpg', 'rb')}. Do not set the Content-Type header manually; Requests handles the boundary generation automatically.


External Sources


Abu Sufyan · Full-stack developer · Founder of WebToolkit Pro Github

Last updated: June 2026

Try the tool

Regex Tester

Test regex patterns with real-time match highlighting — no JS required.

100% client-side·No sign-up·No data sent
Open Tool Free

wtkpro.site

Expert Recommendations

Pro Insights

  • 01.Always use `json=` instead of `data=json.dumps()` in Python Requests when sending JSON payloads to automatically set the correct Content-Type header.
  • 02.Never hardcode authentication credentials from cURL `-u` flags into your Python scripts. Always migrate them to environment variables loaded via `os.environ`.
  • 03.When extracting cURL commands from browser DevTools, use the 'Copy as cURL (bash)' option, as the cmd/PowerShell versions use different quote escaping that is harder to parse.

Frequently Asked Questions

Q. What is the equivalent of cURL -H in Python Requests?

The cURL `-H` or `--header` flag translates to the `headers` dictionary parameter in Python Requests. For example, `requests.get(url, headers={'Authorization': 'Bearer token'})`.

Q. How do I convert cURL -d to Python Requests?

The cURL `-d` or `--data` flag translates to the `data` parameter for form-encoded payloads, or the `json` parameter for JSON payloads in Python Requests.

Q. Does Python Requests support the cURL -u flag for basic auth?

Yes. The `-u` flag (username:password) in cURL maps directly to the `auth=(username, password)` tuple parameter in Python Requests.

#Python#API#cURL#Requests#Backend
AS

Abu Sufyan

Lead Systems Architect & Performance Engineer

Abu Sufyan specializes in V8 execution benchmarking, React architecture, and enterprise-grade technical SEO.

Blog & Journal Archive

All Entries →