-
How to convert cURL commands to Python requests?
Converting cURL commands to Python requests allows you to automate HTTP operations and integrate them into Python scripts. cURL is often used to test API endpoints or fetch data from a server, while Python’s requests library offers a flexible way to handle these tasks programmatically. The key is to translate cURL options (e.g., -X for method, -H for headers, -d for data) into their Python requests equivalents. You can use online tools like curlconverter.com or manually write the conversion.Here’s an example of converting a cURL command into Python:
curl -X POST https://api.example.com/data \ -H "Authorization: Bearer your_api_key" \ -H "Content-Type: application/json" \ -d '{"key": "value"}'
Equivalent Python Code:
import requests url = "https://api.example.com/data" headers = { "Authorization": "Bearer your_api_key", "Content-Type": "application/json" } data = { "key": "value" } response = requests.post(url, headers=headers, json=data) if response.status_code == 200: print("Success:", response.json()) else: print("Error:", response.status_code, response.text)
Python’s requests library provides a clean interface for HTTP methods like GET, POST, PUT, and DELETE. How do you handle complex cURL commands with multiple headers or authentication in Python?
Log in to reply.