Converting HTTP Requests to cURL Commands with Python's curlify Library
The curlify library enables conversion of Python requests objects into executable cURL command strings. This functionality is particularly useful for debugging HTTP intercations and replicating API calls out side of Python environments.
Installation is performed via pip:
pip install curlify
Once installed, import the module and generate a request using the requests library:
import requests
import curlify
endpoint = "https://xxxxx/appco/v1/community/circles?lastCircleId=2&limit=1"
request_headers = {
'timestamp': '1702871644244.104980',
'envId': '1',
'clientId': '5e080c5b48494ef6842f471f56f464cb',
'appVersion': '6.0.00',
'Accept-Language': 'zh',
'sysVersion': '16.5.1',
'clientType': '1',
'User-Agent': 'GoveeHome/6.0.00 (com.ihoment.GoVeeSensor; build:4; iOS 16.5.1) Alamofire/5.6.4',
'timezone': 'Asia/Shanghai',
'Connection': 'keep-alive',
'country': 'CN',
'iotVersion': '1',
'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7ImFjY291bnQiOiJ7XCJjbGllbnRcIjpcIjVlMDgwYzViNDg0OTRlZjY4NDJmNDcxZjU2ZjQ2NGNiXCIsXCJzaWRcIjpcImNKZ25FcVZiR05zazBTdGZsbUpyWFZsUVVQeEQ0aXVSXCIsXCJhY2NvdW50SWRcIjoyNDk5OTIwLFwiZW1haWxcIjpcImdvdmVlMDFAZHJtYWlsLmluXCJ9In0sImlhdCI6MTcwMjg3MTY0NiwiZXhwIjoxNzA4MDU1NjQ2fQ.CKm-xnFJ-JGA_tpVP1INM_SAxpC3TURnbGbfpeFLpyY',
'refreshToken': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7ImFjY291bnQiOiJ7XCJjbGllbnRcIjpcIjVlMDgwYzViNDg0OTRlZjY4NDJmNDcxZjU2ZjQ2NGNiXCIsXCJzaWRcIjpcImNKZ25FcVZiR05zazBTdGZsbUpyWFZsUVVQeEQ0aXVSXCIsXCJhY2NvdW50SWRcIjoyNDk5OTIwLFwiZW1haWxcIjpcImdvdmVlMDFAZHJtYWlsLmluXCJ9In0sImlhdCI6MTcwMjg3MTY0NiwiZXhwIjoxNzE4NDIzNjQ2fQ.Pdb-pdPgcAmsEzF8XdwQNhik2GeDJrt-rBjEqAcwBJI'
}
api_response = requests.get(endpoint, headers=request_headers)
print(api_response.text)
# Convert the request to cURL format
curl_output = curlify.to_curl(api_response.request)
print(curl_output)
The resulting cURL command can be executed directly in a terminal for verification or troubleshooting purposes.