【发布时间】:2015-03-15 02:24:00
【问题描述】:
我正在尝试处理来自 Mailgun 反弹 webhook 的 http post 消息。将其发送到 Mailgun 的 Postbin 服务时,当然会找到所有数据。但是我现在将该 POST 发送到我的本地主机服务器以用于开发目的,而我得到的只是空的 json 数组。我使用测试 Webhook。
除了我们的主要服务之外,我们的意图是尽可能保持简单。我开始使用 nodejs/expressjs 创建独立的 web 服务,作为中继接收来自 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");
});
我在 Mailgun 的测试 Webhook 中使用像 http://mylocalhostwithpublicip.com:5000/webhooks/mailgun 这样的 url 运行它
代码结构复制自https://github.com/1lobby/mailgun-js。可能我在这里遗漏了一些基本的东西,因为我自己无法弄清楚。
【问题讨论】:
-
console.dir(req.headers['content-type'])放置在您的路由处理程序中时会显示什么? -
它给了
'multipart/form-data; boundary=330afb68-af25-4d0a-89e2-44a10be325fd'