I wrote an Express middleware to retrieve the raw body from the request, and I set it before body-parser middleware.
My custom middleware is calling req.setEncoding('utf8')
, but this causes the following body-parser error:
Error: stream encoding should not be set
at readStream (/node_modules/body-parser/node_modules/raw-body/index.js:211:17) at getRawBody (/node_modules/body-parser/node_modules/raw-body/index.js:106:12) at read (/node_modules/body-parser/lib/read.js:76:3) at jsonParser (/node_modules/body-parser/lib/types/json.js:127:5)
Here is my code:
var express = require('express');
var bodyParser = require('body-parser')
function myMiddleware() {
return function(req, res, next) {
req.rawBody = '';
req.setEncoding('utf8');
req.on('data', function(chunk) {
req.rawBody += chunk;
});
req.on('end', function() {
next();
});
}
}
var app = express();
app.use(myMiddleware());
app.use(bodyParser.json());
var listener = app.listen(3000, function() {
});
app.get('/webhook/', function (req, res) {
res.sendStatus(200);
});
Is there a way to unset the encoding? Is there another way to retrieve the raw body, but still use body-parser after it?
res.sendStatu(200);
as well. – ProthalamionsetEncoding()
, I thought I was required too, because without it, the app hangs. Now I understand that whoever tries to read the body for second time will hang, in this case body-parser. – Pocknext()
isn't getting called until afterend
is emitted on the data. Try just setting the event handlers and then callingnext()
at the end, not in a handler. – ProthalamionmyMiddleware
is never getting called, so next is never getting called. – Prothalamion