2

I am trying to learn more about using http requests and the Mailchimp API, but I cannot seem to figure out how to add a member to list using a post request. I have tried multiple configurations, and I guess I made some headway since my response went from a 405, to a 401, now I'm getting a 400. I assume this means that I am being authenticated, but I am formatting the request incorrectly.

I have gotten it to work the python mailchimp library, but I want to actually learn how to use the HTTP requests. I could find very few examples of using python requests with Mailchimp.

(obviously I put in my actual list_id, my_username, and 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)

This is my response:

>>> response
<Response [400]>

I'm stumped....what am I doing wrong?

This worked:

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)
Fraxr
  • 125
  • 2
  • 10
  • Check the response content `response.content` to see whether the error message explains the problem. – Alasdair May 19 '17 at 11:12
  • You are json encoding the data twice. Use `requests.post(..., data=json.dumps(data1))` or `requests.post(..., json=data1)`. – Alasdair May 19 '17 at 11:14
  • Thanks @Alasdair, you pointed me in the right direction. I checked response.content and it said `...Schema describes object, string found instead` due to double encoding the json data. I fixed it (which I had previously tried both ways) and it said `blah@blah.com looks fake or invalid, please enter a real email address.","instance`, which is why I couldn't get it to work either way. I put in a real address and it works now. Thanks again...another learning experience. – Fraxr May 19 '17 at 17:03
  • Glad you got it working! Please add you solution as an answer, instead of updating the question. It makes it easier to see that your problem has been solved. – Alasdair May 19 '17 at 17:25

1 Answers1

5

This worked:

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)

My problems were #1 I was double encoding the json data and #2 mailchimp recognizes obviously BS email addresses like blah@blah.com

Thanks @Alasdair

Fraxr
  • 125
  • 2
  • 10