How do you access an Amazon SNS post body with Express / Node.js -
i'm in process of rebuilding php app in node.js on top of express framework.
one part of application callback url amazon sns notification posted to.
the post body sns read in following way in php (which works):
$notification = json_decode(file_get_contents('php://input'));
in express have tried following:
app.post('/notification/url', function(req, res) { console.log(req.body); });
however, watching console, logs following when post made:
{}
so, repeat question: how access amazon sns post body express / node.js
another approach fix content-type header.
here middleware code this:
exports.overridecontenttype = function(){ return function(req, res, next) { if (req.headers['x-amz-sns-message-type']) { req.headers['content-type'] = 'application/json;charset=utf-8'; } next(); }; }
this assumes there file called util.js located in root project directory with:
util = require('./util');
in app.js , invoked including:
app.use(util.overridecontenttype());
before
app.use(express.bodyparser());
in app.js file. allows bodyparser() parse body properly...
less intrusive , can access req.body normally.
Comments
Post a Comment