Hello Node-RED community,
I've been trying to access the raw request body in a Node-RED project, specifically for processing Stripe webhook events, which require access to the raw body to verify their signature. I've tried several approaches, but none have been successful so far. I am seeking your guidance and expertise to help me solve this issue.
Here is what I've tried so far:
- Using the
httpNodeMiddleware
in thesettings.js
file to intercept the request and parse the raw body using theraw-body
package:
const getRawBody = require('raw-body');
module.exports = {
// ...
httpNodeMiddleware: function (req, res, next) {
if (req.originalUrl === '/stripe/webhook' && req.method === 'POST') {
req.skipRawBodyParser = true;
getRawBody(req, {
length: req.headers['content-length'],
limit: '1mb',
encoding: 'utf8'
}, function (err, string) {
if (err) {
return next(err);
}
req.body = string;
next();
});
} else {
next();
}
},
// ...
};
This approach resulted in the following error: InternalServerError: stream is not readable
.
- Trying different stream handling approaches like
on-finished
andbusboy
packages, but they either produced the same error or were not suitable for handling raw JSON payloads.
const getRawBody = require('raw-body');
const onFinished = require('on-finished');
module.exports = {
// ...
httpNodeMiddleware: function (req, res, next) {
if (req.originalUrl === '/stripe/webhook' && req.method === 'POST') {
req.skipRawBodyParser = true;
console.log('httpNodeMiddleware', req.originalUrl);
// Ensure the request is fully processed before parsing the raw body
onFinished(req, function (err) {
if (err) {
return next(err);
}
// Parse the raw body
getRawBody(req, {
length: req.headers['content-length'],
limit: '1mb',
encoding: 'utf8'
}, function (err, string) {
if (err) {
return next(err);
}
req.body = string;
next();
});
});
} else {
next();
}
},
// ...
};
I am looking for a solution to access the raw request body in Node-RED without relying on additional dependencies or packages. Any suggestions, guidance, or examples would be greatly appreciated. Thank you in advance for your help!