I'm using NodeJS to call the new MailChimp 3.0 API in order to add an email to a list. While I can get it working via POSTman, I'm having a hard time with Node's http:
var http = require('http');
var subscriber = JSON.stringify({
    "email_address": "[email protected]", 
    "status": "subscribed", 
    "merge_fields": {
        "FNAME": "Tester",
        "LNAME": "Testerson"
    }
});
var options = {
    host: 'https://us11.api.mailchimp.com',
    path: '/3.0/lists/<myListID>/members',
    method: 'POST',
    headers: {
        'Authorization': 'randomUser myApiKey',
        'Content-Type': 'application/json',
        'Content-Length': subscriber.length
    }
}
var hreq = http.request(options, function (hres) {  
    console.log('STATUS CODE: ' + hres.statusCode);
    console.log('HEADERS: ' + JSON.stringify(hres.headers));
    hres.setEncoding('utf8');
    hres.on('data', function (chunk) {
            console.log('\n\n===========CHUNK===============')
            console.log(chunk);
            res.send(chunk);
    });
    hres.on('end', function(res) {
            console.log('\n\n=========RESPONSE END===============');
    });
    hres.on('error', function (e) {
            console.log('ERROR: ' + e.message);
    }); 
});
hreq.write(subscriber);
hreq.end();
Rather than getting even some sort of JSON error from Mailchimp, however, I'm getting HTML: 400 Bad Request
Is it clear at all what I"m doing wrong here? It seems pretty simple, yet nothing I've tried seems to work.
A few additional thoughts:
It turns out this had a very simple solution: the "host" property of the options object needed to have only the domain name. IE, remove the "https://" protocol:
var options = {
    host: 'us11.api.mailchimp.com',
    path: '/3.0/lists/<myListID>/members',
    method: 'POST',
    headers: {
        'Authorization': 'randomUser myApiKey',
        'Content-Type': 'application/json',
        'Content-Length': subscriber.length
    }
}
                        Try this , its working fine for Me.
var request = require('request');
function mailchimpAddListCall(email, cb){
var subscriber = JSON.stringify({
        "email_address": email,
        "status": "subscribed"
    });
request({
             method: 'POST',
             url: 'https://us13.api.mailchimp.com/3.0/lists/<Your list id>/members',
             body: subscriber,
             headers:
                    {
                        Authorization: 'apikey <your Mailchimp API key>',
                        'Content-Type': 'application/json'
                    }
         },
          function(error, response, body){
            if(error) {
                cb(err, null)
            } else {
                var bodyObj = JSON.parse(body);
                console.log(bodyObj.status);
                if(bodyObj.status === 400){
                    cb(bodyObj.detail, null);
                }
                var bodyObj = JSON.parse(body);
                cb(null, bodyObj.email_address +" added to list.");
            }
        });
}
request is a node module, that you'll need to install into your package.json. npm install --save request
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With