Mailgun webhook POST机身似乎是空的

Qri*_*ila 14 webhooks node.js mailgun

我试图处理来自Mailgun bounce webhook的http帖子消息.当将其发送到Mailgun的Postbin服务时,当然会找到所有数据.但我现在将POST发送到我的localhost服务器进行开发,我得到的只是空的json数组.我使用Test Webhook.

除了我们的主要服务之外,意图是保持这种简单.那是因为我开始使用nodejs/expressjs创建独立的webservice作为中继来接收来自Mailgun的电子邮件退回的POST消息,并通知管理员有关退回的电子邮件地址.

现在我无法弄清楚为什么我没有获得与Postbin中可见的数据相同的数据.

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mailgun = require('mailgun-js')({apiKey: 'key-...', domain: 'mymailgundomain.com'});

app.use(bodyParser.urlencoded({
  extended: true
}));

function router(app) {
  app.post('/webhooks/*', function (req, res, next) {
    var body = req.body;

    if (!mailgun.validateWebhook(body.timestamp, body.token, body.signature)) {
      console.error('Request came, but not from Mailgun');
      res.send({ error: { message: 'Invalid signature. Are you even Mailgun?' } });
      return;
    }

    next();
  });

  app.post('/webhooks/mailgun/', function (req, res) {
    // actually handle request here
    console.log("got post message");
    res.send("ok 200");
  });
}

app.listen(5000, function(){
  router(app);
  console.log("listening post in port 5000");
});
Run Code Online (Sandbox Code Playgroud)

我正在运行这个来自Mailgun的Test Webhook,使用网址,如http://mylocalhostwithpublicip.com:5000/webhooks/mailgun

代码结构从https://github.com/1lobby/mailgun-js复制.可能我在这里缺少一些基本的东西,因为我无法弄清楚自己.

msc*_*dex 15

您没有看到req.body填充的原因是因为该body-parser模块不支持multipart/form-data请求.对于这些类型的请求,你需要如不同的模块multer,busboy/ connect-busboy,multipartyformidable.

  • 对于...的爱...谢谢。用火锅拿到。 (2认同)

小智 6

如果您正在使用内容类型(通过日志记录显示console.dir(req.headers['content-type']))'application/x-www-form-urlencoded',请body-parser尝试添加以下行:

    bodyParser = require('body-parser')
    app.use(bodyParser.urlencoded({ extended: false }))
Run Code Online (Sandbox Code Playgroud)