【问题标题】:How to do a post request to MailChimp api using Axios package如何使用 Axios 包向 MailChimp api 发出 post 请求
【发布时间】:2020-11-05 19:34:33
【问题描述】:

我最近开始使用 Axios 包。我正在处理我的时事通讯注册页面,并使用它向 MailChimp Api 发送新订阅者的 Post 请求,但我收到了 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);
    });

【问题讨论】:

    标签: node.js api axios mailchimp


    【解决方案1】:

    使用来自 npm 的 @mailchimp/mailchimp_marketing 代替 Axios,并且可以重现如下:

    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"));
    });
    

    【讨论】:

      猜你喜欢
      • 2021-11-12
      • 2017-10-16
      • 2020-07-28
      • 2022-11-02
      • 2020-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多