Tasos Tasos - 1 month ago 9
Python Question

Use Python Requests on ChartMogul API

I try to use the API of ChartMogul with Python. And since they don't have a client library for it, I need to do it myself. This is the API example:

curl -X POST "https://api.chartmogul.com/v1/import/customers" \
-u YOUR_ACCOUNT_TOKEN:YOUR_SECRET_KEY \
-H "Content-Type: application/json" \
-d '{
"data_source_uuid": "ds_fef05d54-47b4-431b-aed2-eb6b9e545430",
"external_id": "cus_0001",
"name": "Adam Smith",
"email": "adam@smith.com",
"country": "US",
"city": "New York"
}'


This is what I do:

import requests

url = "https://api.chartmogul.com/v1/import/customers"
data_source_uuid = "the uuid I get from my api data source"
token = 'my token'
key= 'my key'
payload = {
"data_source_uuid": data_source_uuid,
"external_id": "cus_0001",
"name": "Adam Smith",
"email": "adam@smith.com",
"country": "US",
"city": "New York"
}

headers = {"Content-Type": "application/json"}
res = requests.post(url,
auth=(token, key),
data=payload,
headers=headers)


But I have a response 400 (bad request). What is wrong with it?

Answer

Try

data=json.dumps(payload),

instead of

data=payload,
Comments