root/learning-hub/api-automation/convert-curl-to-python
← Back to Learning Hub
API & AUTOMATION•6 min read•Beginner••100% Client-Side Verified

How to Convert cURL Commands to Python requests — Complete Guide with Code

Step-by-step guide to converting cURL commands into clean, production-ready Python requests code. Covers GET, POST, JSON, custom headers, auth, and error handling.

#cURL#Python#API#Requests#Automation

Interactive cURL to Python Translator

Live Interactive Sandbox
100% Client-Side

Translates shell flags into clean requests.post dictionary arguments. Test the preset input below or customize it before launching into the full workspace.

cURL Bash Command:
Ready for advanced parsing, syntax error highlighting & bulk export?
Open in Full Workspace (cURL to Python)

cURL is the universal language of API documentation and network debugging. When you copy an API request from Chrome DevTools, Postman, or Swagger, it almost always comes formatted as a curl command.

However, when building automation scripts or backend services, you need to convert that command into clean, readable Python code using the standard requests library.

This guide provides a comprehensive mapping of all cURL parameters to Python requests.


1. Basic GET Request

cURL:

curl https://api.example.com/v1/users

Python:

import requests

response = requests.get('https://api.example.com/v1/users')

print(f"Status Code: {response.status_code}")
data = response.json()
print(data)

2. Query Parameters

Instead of manually constructing URL query strings, pass a dictionary to the params argument in Python.

cURL:

curl "https://api.example.com/v1/search?query=developer&limit=20&sort=desc"

Python:

import requests

params = {
    'query': 'developer',
    'limit': 20,
    'sort': 'desc'
}

response = requests.get('https://api.example.com/v1/search', params=params)
print(response.url)

3. POST Requests with JSON Payloads (-d / --data)

When sending JSON data in Python, use the json= parameter rather than data=:

import requests

payload = {
    'name': 'Saad',
    'role': 'engineer',
    'active': True
}

response = requests.post('https://api.example.com/v1/users', json=payload)
print(response.status_code)

Live Tool: cURL to Python Requests

Client-Side Engine

Translate bash cURL commands into Python `requests` code.

Launch Tool Workspace

Frequently Asked Questions (FAQ)

Passing a dictionary to json= automatically serializes it to JSON and attaches the 'Content-Type: application/json' header. Passing it to data= sends it as application/x-www-form-urlencoded.

Related Guides & References

view learning hub →