Hey folks, I’m having trouble with Mailgun’s bounce webhook. I set up a simple Node.js server using Express to handle POST requests from Mailgun. It works fine when I use Mailgun’s Postbin service, but when I point it to my local server, I get an empty JSON array. I’m using the Test Webhook feature.
Here’s a snippet of my code:
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const mailgunVerifier = require('mailgun-verifier')({secretKey: 'my-secret-key', domain: 'example.com'});
app.use(bodyParser.urlencoded({ extended: true }));
function setupRoutes(app) {
app.post('/hooks/*', (req, res, next) => {
const data = req.body;
if (!mailgunVerifier.checkWebhook(data.timestamp, data.token, data.signature)) {
console.error('Invalid request source');
res.status(400).json({ error: 'Invalid signature' });
return;
}
next();
});
app.post('/hooks/bounces', (req, res) => {
console.log('Received webhook');
res.sendStatus(200);
});
}
app.listen(3000, () => {
setupRoutes(app);
console.log('Server running on port 3000');
});
I’m testing with the URL http://my-public-ip:3000/hooks/bounces. Any ideas why I’m not getting the same data as in Postbin? Am I missing something obvious here?
I ran into a similar issue with Mailgun webhooks a while back. One thing that helped me was to double-check the webhook URL in my Mailgun settings. Sometimes, a tiny typo can cause the data to go nowhere. Also, have you tried using a tool like Postman to simulate the webhook POST request? It’s great for debugging these kinds of issues.
Another thing to consider is SSL. If your server is expecting HTTPS and you’re testing with HTTP, that could explain the empty payload. You might want to temporarily disable any SSL requirements for testing.
Lastly, don’t forget to check your Mailgun logs. They often provide valuable insights into what’s happening with your webhooks. It’s saved me hours of head-scratching more than once.
Keep at it, and you’ll crack it soon enough!
I’ve encountered similar issues with Mailgun webhooks before. One thing to check is your Content-Type header. Mailgun typically sends webhook data as application/x-www-form-urlencoded, but your current setup might be expecting JSON. Try modifying your bodyParser middleware to handle both:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
Also, ensure your server is accessible from the internet. Some ISPs block incoming connections on certain ports. You might want to try using a service like ngrok to expose your local server securely. This can help rule out any network-related issues.
hey jackhero, have u tried logging the entire req object? sometimes the data might be in a different place than expected. also, double-check ur firewall settings - they could be blocking incoming requests. lastly, make sure ur public ip is correctly forwarded to ur local machine. good luck troubleshooting!