使用节点 https 将订阅者添加到 mailchimp 3.0

Posted

技术标签:

【中文标题】使用节点 https 将订阅者添加到 mailchimp 3.0【英文标题】:Adding subscriber to mailchimp 3.0 with node https 【发布时间】:2020-08-20 01:16:19 【问题描述】:

这是我的 app.js 文件:

const express = require("express");
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;


  var data = 
    members: [
      "email_address": email,
      "status": "subscribed",
      "merge_fields": 
        "FNAME": firstName,
        "LNAME": lastName
      
    ]
  ;

  var jsondata = JSON.stringify(data);

  const url = `https://us8.api.mailchimp.com/3.0/lists/<list id>`; 

  const options = 
    method: "POST",
    auth: "jay:<my API key>"
  ;

  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("You are now live");
);

当我的表单提交时,我收到下面提到的错误状态 400:


  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: '25fa1c2d-0740-4e45-9c9c-43c51cca5862',
  errors: [
    
      field: 'email_address',
      message: 'This value should not be blank.'
    
  ]


  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: 'dc94871f-2eba-417a-ba0f-b913de939b64',
  errors: [
    
      field: 'email_address',
      message: 'This value should not be blank.'
    
  ]

我以为我的表单没有提交电子邮件地址,但是当我在控制台记录该值时,它会返回用户输入的内容。我阅读了文档,但很难理解错误的含义......如果你能解决这个问题,这将是一个很大的帮助。

谢谢。

【问题讨论】:

【参考方案1】:

这行得通。

const express = require("express");

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;


  var data = 
    members:[ 
      email_address: email,
      status: "subscribed",
      merge_fields: 
        FNAME: firstName,
        LNAME: lastName
      
    ]
  ;

  var jsondata = JSON.stringify(data);

  const url = `https://us8.api.mailchimp.com/3.0/lists/de7bf306b4`; 

  const options = 
    method: "POST",
    body: data,
    auth: "#######"
  ;


  const request = https.request(url, options, function(response)

    response.on("data", function (data) 
      console.log(JSON.parse(data));
    );

    if(response.statusCode === 200)
      res.sendFile(__dirname + "/success.html");
     else 
      res.sendFile(__dirname+"/failure.html");
    



  );

  request.write(jsondata);
  request.end();
  


);

app.post("/failure", function(req,res)
  res.redirect("/");
);

app.listen(process.env.PORT || 3000, function()
  console.log("You are now live");
);

【讨论】:

以上是关于使用节点 https 将订阅者添加到 mailchimp 3.0的主要内容,如果未能解决你的问题,请参考以下文章

使用 paypal 节点 sdk 将自定义字段添加到计费协议

使用注册表单而不是 API 将 MailChimp 订阅者添加到组

使用 Mailchimp 的 API 将订阅者添加到列表

使用 Mailchimp 的 API v3 将订阅者添加到列表

MailChimp Ajax 提交不会将订阅者添加到列表中

如何使用prezto将节点版本添加到zsh提示符