我正在尝试了解有关使用http请求和Mailchimp API的更多信息,但我似乎无法弄清楚如何使用发布请求将成员添加到列表中。我已经尝试了多种配置,我想我取得了一些进展,因为我的响应从405到401,现在我得到了400.我认为这意味着我正在通过身份验证,但我正在格式化请求
我已经得到它来工作python mailchimp库,但我想实际学习如何使用HTTP请求。我可以找到很少的使用Mailchimp的python请求的例子。
(显然我放入了我的实际list_id,my_username和my_apikey)
import requests, json
members_url = 'https://us15.api.mailchimp.com/3.0/lists/XXXXXXXX/members/'
auth = ('my_username', 'my_apikey')
headers = {'Content-Type': 'application/json'}
data1 = {
'email_address':'blah@blah.com',
'status':'subscribed',
'merge_fields':{
'FNAME':'John',
'LNAME':'Doe'
}
}
payload = json.dumps(data1)
response = requests.post(members_url, auth=auth, headers=headers, json=payload)
这是我的回答:
>>> response
<Response [400]>
我很难过......我做错了什么?
这有效:
import requests, json
members_url = 'https://us15.api.mailchimp.com/3.0/lists/XXXXXXXX/members/'
auth = ('my_username', 'my_apikey')
headers = {'Content-Type': 'application/json'}
data1 = {
'email_address':'<a real email address>', #it could tell 'blah@blah.com was fake'
'status':'subscribed',
'merge_fields':{
'FNAME':'John',
'LNAME':'Doe'
}
}
response = requests.post(members_url, auth=auth, headers=headers, json=data1)
答案 0 :(得分:2)
这有效:
import requests, json
members_url = 'https://us15.api.mailchimp.com/3.0/lists/XXXXXXXX/members/'
auth = ('my_username', 'my_apikey')
headers = {'Content-Type': 'application/json'}
data1 = {
'email_address':'<a real email address>', #it could tell 'blah@blah.com was fake'
'status':'subscribed',
'merge_fields':{
'FNAME':'John',
'LNAME':'Doe'
}
}
response = requests.post(members_url, auth=auth, headers=headers, json=data1)
我的问题是#1我对json数据进行了双重编码,#2 mailchimp明显识别出BS电子邮件地址,如blah@blah.com
谢谢@Alasdair