NodeJS 如何使用Axios软件包向MailChimp API发送post请求

qjp7pelc  于 2023-05-22  发布在  Node.js
关注(0)|答案(2)|浏览(121)

我最近开始使用Axios包。我正在我的新闻通讯注册页面上工作,并使用它向MailChimpAPI发送一个新订阅者的帖子请求,我得到了一个400错误。谁能帮我写一下这个代码。

var Data = {
    members: [{
        email_address: email,
        status: "suscribed",
        merge_fields: {
            FNAME: firstName,
            LNAME: lastName
        }
    }]
};

var jsonData=JSON.stringify(Data);  
axios.post(
        "https://***.api.mailchimp.com/3.0/lists/**********",{
            data: jsonData
        },
        {
            auth: {
                username: "any_name",
                password:"API_keys"
            }
        })

    .then(function(response) {
        console.log(response);
    })
    .catch(function(error) {
        console.log(error);
    });
blmhpbnm

blmhpbnm1#

而不是Axios,使用npm的@mailchimp/mailchimp_marketing,可以如下复制:

const mailchimp = require("@mailchimp/mailchimp_marketing");
const express = require("express");
const bodyParser = require("body-parser");
const app = express();

//Using body-parser
app.use(express.urlencoded({extended:true}));

//The public folder which holds the CSS
app.use(express.static("public"));

//Listening on port 3000 and if it goes well then logging a message saying that the server is running
app.listen(3000, function () {
console.log("Server is running at port 3000");
});

//Sending the signup.html file to the browser as soon as a request is made on localhost:3000
app.get("/", function (req, res) {
res.sendFile(__dirname + "/signup.html");
});

//Setting up MailChimp
mailchimp.setConfig({
apiKey: "YOUR API KEY-usX",
server: "usX"
});

//As soon as the sign in button is pressed execute this

app.post("/", function (req,res) {
const firstName = req.body.fName;
const lastName = req.body.lName;
const email = req.body.email;

//Add a contact to an audience
const listId = "YOUR AUDIENCE ID";
const subscribingUser = {
  firstName: firstName,
  lastName: lastName,
  email: email
};
//Uploading the data to the server
async function run() {
  const response = await mailchimp.lists.addListMember(listId, {
  email_address: subscribingUser.email,
  status: "subscribed",
  merge_fields: {
  FNAME: subscribingUser.fName,
  LNAME: subscribingUser.lName
  }
});
//If all goes well logging the contact's id
res.sendFile(__dirname + "/success.html")
console.log(`Successfully added contact as an audience member. The contact's id is ${response.id}.`);
}
//Running the function and catching the errors (if any)

  run().catch(e => res.sendFile(__dirname + "/failure.html"));
});
mklgxw1f

mklgxw1f2#

您可以像下面这样向MailChimp发出发布请求:

const url = "https://{dc_here}.api.mailchimp.com/3.0/lists/{list_id}"
const data = {
    members:[{email_address: "sachin.test.11@gmail.com",status:"subscribed"}]
}
    
const options = {
auth: {
    username: "anystring",
    password: "{mailchimp_key_here}"
    },
}
const axios_response = await axios.post(url, data, options);
console.log(axios_response.data);

**注意:**将{dc_here}替换为有效dc。示例:us-21

将{list_id}替换为有效的列表ID。示例:7762 d 0 b7 cd
将{mailchimp_key_here}替换为有效密钥。示例:fd 14 c93 dadd 62130 afc 7 ee 406 b45 d3 a2-us 21
以上三个细节你可以在Mailchimp网站上找到

相关问题