Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mailgun webhook POST body seems empty

I'am trying to handle http post message from Mailgun bounce webhook. When sending it to Mailgun's Postbin service all data is found of course. But I'm now sending that POST to my localhost server for development purposes and all I get is empty json array. I use Test Webhook.

Intent is to keep this simple as possible besides our main service. That for I started using nodejs/expressjs to create stand alone webservice to work as relay to receive POST messages of email bounces from Mailgun and inform admins about bounced email addresses.

Now I can't figure why I don't get the same data as is visible in 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");
});

I'm running this from Mailgun's Test Webhook using url like http://mylocalhostwithpublicip.com:5000/webhooks/mailgun

Code structure is copied from https://github.com/1lobby/mailgun-js. Probably I'm missing something fundamental here as I can't figure it out myself.

like image 847
Qrila Avatar asked Jan 16 '15 13:01

Qrila


1 Answers

The reason you're not seeing req.body populated is because the body-parser module does not support multipart/form-data requests. For those kinds of requests you need a different module such as multer, busboy/connect-busboy, multiparty, or formidable.

like image 158
mscdex Avatar answered Oct 11 '22 05:10

mscdex