I just started this all API thing- I'm trying to upload data to Mailchimp using their API. I'm not getting any error while running my server, but the data won't get updated (I do get the values from the html form and they get displayed in the hyper terminal) that's my code
const bodyParser=require("body-parser");
const request=require("request");
const https=require("https");
const app=express();
app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended:true}));
app.get("/",function(req,res)
{
res.sendFile(__dirname+"/signup.html");
});
app.post("/",function(req,res)
{
const firstName=req.body.Fname;
const lastName=req.body.Lname;
const email=req.body.email;
console.log(firstName,lastName,email);
const data=
{
members:[
{
email_adress:email,
status:"subscribed",
merge_fields:
{
FNAME:firstName,
LNAME:lastName
}
}
]
};
const jsonData=JSON.stringify(data);
const url="https://us8.api.mailchimp.com/3.0/lists/b8e36ded50";
const options=
{
methods:"POST",
auth:"lidor:56599763339503b7bac161913940d98d-us8"
}
const request=https.request(url,options,function(response)
{
response.on("data",function(data)
{
console.log(JSON.parse(data));
})
})
request.write(jsonData);
request.end();
});
app.listen(3000,function()
{
console.log("server has started on port 3000");
});
please help me !!
Try this instead
var mailchimpInstance = 'your_prefix_server',
listUniqueId = 'your_listId',
mailchimpApiKey = 'your_api_key';
app.post('/signup', function (req, res) {
request
.post('https://' + mailchimpInstance + '.api.mailchimp.com/3.0/lists/' + listUniqueId + '/members/')
.set('Content-Type', 'application/json;charset=utf-8')
.set('Authorization', 'Basic ' + new Buffer.from('any:' + mailchimpApiKey ).toString('base64'))
.send({
'email_address': req.body.email,
'status': 'subscribed',
'merge_fields': {
'FNAME': req.body.firstName,
'LNAME': req.body.lastName
}
})
.end(function(err, response) {
if (response.status < 300 || (response.status === 400 && response.body.title === "Member Exists")) {
res.send('Signed Up!');
} else {
res.send('Sign Up Failed :(');
}
});
});
from https://www.codementor.io/@mattgoldspink/integrate-mailchimp-with-nodejs-app-du10854xp
also he uses "superagent" module instead of "request" module. It works!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.