edgar piet
edgar piet

Reputation: 67

Send POST request to mailchimp API with Python

I am trying to send a POST request to the mailchimp api to add a new member to a list, but I keep getting an error saying that the api key is missing. When I send a get request I include the key in the URL and it works normal.

From the Mailchimp documentation it looks as though the api key should be part of the request and the parameters (email_address and status) part of the body, but I don't understand how to do that using requests. (I know there is a mailchimp module for Python, but I have some custom things to do and I want to get my head around this)

This is the POST I am trying to get going:

import requests
url="https://us15.api.mailchimp.com/3.0/lists/xxxxx/members/"

header ={"Authorization":"apikey xxxxx",
"email_address":"[email protected]",
"status":"subscribed"}

r=requests.post(url,header)
print r.text

The error I get reads: {"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"API Key Missing","status":401,"detail":"Your request did not include an API key.","instance":""}

I also tried to put this request in Postman where you can easily separate the header and the body, but it gives the same response.

I also tried it using the Mailchimp3 package, but that gives me a bad request. The code I used is as follows:

from mailchimp3 import MailChimp

client = MailChimp('[email protected]', '{}-{}'.format('xxxxxxxxxxxxxxxxxxxxxxx','us15'))

client.lists.members.create('2be23de2cc', {'email_address': '[email protected]','status': 'subscribed'})

However, I get the following error: requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://us15.api.mailchimp.com/3.0/lists/2be23de2cc/members

Upvotes: 2

Views: 8416

Answers (2)

Vinicius Ronconi
Vinicius Ronconi

Reputation: 181

Why don't you give this package a try? https://github.com/charlesthk/python-mailchimp

You can do that by:

from mailchimp3 import MailChimp
client = MailChimp('my_user', '{}-{}'.format(access_token, data_center))
client.lists.members.create('my_list_id', {'email_address': '[email protected]', 'status': 'subscribed'})

When testing it, please do not test with fake emails because Mailchimp has a kind of global ban list to ignore the emails used by spammers. In this case, it will return a HTTP 400.

Upvotes: 6

Burhan Khalid
Burhan Khalid

Reputation: 174708

Ignoring the typo in your code header ={"Authorization","apikey xxxxx",, you don't send credentials or the data in the header. You first need to authenticate, then send the subscriber information as a POST payload, like this:

import pprint
import requests

username = 'foo'
apikey = 'sekret-key-goes-here'
url = "https://us15.api.mailchimp.com/3.0/lists/xxxxx/members/"

post_params = {'email_address': '[email protected]', 'status': 'subscribed'}

r = requests.post(url, auth=(username, apikey), json=post_params)
r.raise_for_status()

results = r.json()
pprint.pprint(results)

Upvotes: 4

Related Questions