【问题标题】:Webhook for Mailgun POST?Mailgun POST 的 Webhook?
【发布时间】:2015-01-28 22:00:04
【问题描述】:

我正在尝试通过 Mailgun webhook 将电子邮件消息作为 JSON(由 Mailgun 解析)存储在 Mongo.Collection 中。我设置了一个 Iron-router 服务器端路由来处理请求,但 this.request.body 是空的。我正在使用 Mailgun 的“Send A Sample POST”来发送请求,并且使用例如 POST 看起来很好。 http://requestb.in/。我希望 request.body 有数据,如How do I access HTTP POST data from meteor? 中所述。我做错了什么?

Router.map(function () {
  this.route('insertMessage', {
    where: 'server',
    path: '/api/insert/message',
    action: function() {
      var req = this.request;
      var res = this.response;

      console.log(req.body);
      ...

【问题讨论】:

    标签: meteor webhooks iron-router mailgun


    【解决方案1】:

    我不确定这是正确的语法。您是否尝试过使用Router.route

    Router.route('insertMessage', 
      function () {
        // NodeJS request object
        var request = this.request;
    
        // NodeJS  response object
        var response = this.response;
    
        console.log("========= request: =============");
        console.log(request);
    
        // EDIT: also check out this.params object
        console.log("========= this.params: =============");
        console.log(this.params);
    
        // EDIT 2: close the response. oops.
        return response.end();
      }, 
      {
        where: 'server',
        path: '/api/insert/message'
      }
    );
    

    【讨论】:

    • 我看到了this.request 对象,但是它很大,并且不清楚相关信息在哪里。而且 this.params 没有任何帮助。 This (link) 是我希望轻松提取的那种数据(requestb.in POST bin,可能会在 48 小时后过期)。
    【解决方案2】:

    我认为问题在于 Mailgun 发送多部分 POST 请求,例如它发送“字段”和“文件”(即附件),并且 Iron-router 不会为多部分请求设置正文解析器。这个问题在 Iron-router 的 Github 问题上讨论 herehere。我发现 this 评论特别有用,现在我可以正确解析 Mailgun 的示例 POST。

    为了让它发挥作用,在一个新的 Meteor 项目中,我做到了

    $ meteor add iron:router
    $ meteor add meteorhacks:npm
    

    在根级 packages.json 我有

    {
        "busboy": "0.2.9"
    }
    

    使用meteorhacks:npm 包,使“busboy”npm 包可通过Meteor.npmRequire 在服务器上使用。

    最后,在server/rest-api.js 我有

    Router.route('/restful', {where: 'server'})
      .post(function () {
        var msg = this.request.body;
        console.log(msg);
        console.log(_.keys(msg));
        this.response.end('post request\n');
      });
    
    var Busboy = Meteor.npmRequire("Busboy");
    
    Router.onBeforeAction(function (req, res, next) {
      if (req.method === "POST") {
        var body = {}; // Store body fields and then pass them to request.
        var busboy = new Busboy({ headers: req.headers });
        busboy.on("field", function(fieldname, value) {
          body[fieldname] = value;
        });
        busboy.on("finish", function () {
          // Done parsing form
          req.body = body;
          next();
        });
        req.pipe(busboy);
      }
    });
    

    通过这种方式,我可以忽略文件(即,我没有 busboy.on("file" 部分)并在我的路由中使用 this.request.body,其中所有 POST fields 都是 JSON。

    【讨论】:

      猜你喜欢
      • 2020-12-25
      • 2015-03-15
      • 2015-11-13
      • 2013-01-19
      • 2014-11-08
      • 1970-01-01
      • 2016-12-01
      • 2017-12-10
      • 1970-01-01
      相关资源
      最近更新 更多