I am tryiong to use the mailchimp API in order to subscribe a user to an existing list.
Using the node https request, I send the following request to the mailchimp server:
const url = `https://us7.api.mailchimp.com/3.0/lists/<list_id>/members?skip_merge_validation=true'`;
const options = {
method: "POST",
auth: "<name>:<API Key>",
};
const request = https.request(url, options, (response) => {
response.on("data", (data) => {
console.log(JSON.parse(data));
if (data.status === 200) {
res.redirect("/sucess");
} else {
res.redirect("/failure");
}
});
});
request.write(JSON.stringify(user_data));
request.end();
});
Before making the request, I console.log the user data collected from the front-end.
console.log("USER DATA : " + JSON.stringify(user_data));
and this is the answer i am getting from the API server :
USER DATA : {"members":[{"email_address":"a.b@email.com","status":"subscribed","merge_fields":{"FNAME":"A","LNAME":"B"}}]}
{
type: 'http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/',
title: 'Invalid Resource',
status: 400,
detail: "The resource submitted could not be validated. For field-specific details, see the 'errors' array.",
instance: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
errors: [
{
field: 'email_address',
message: 'This value should not be blank.'
}
]
}
As you can see in the log, the email_address field is not blank.
Has anyone encountered this problem before ? any Ideas ?
Thanks in advance
I've found the problem, it was related to my user data structure. It is now working with the following code
const user_data = {
email_address: req.body.email,
status: "subscribed",
merge_fields: {
FNAME: req.body.fName,
LNAME: req.body.lName,
}
}