Optionally allow json middleware to not check the content-type

This commit is contained in:
Johannes Zellner
2026-01-26 19:53:01 +01:00
parent dbd5810a08
commit 0889c1531e
+2 -2
View File
@@ -7,12 +7,12 @@ function _mime(req) {
return str.split(';')[0];
}
exports = module.exports = function (options) {
exports = module.exports = function (options, forceContentType = true) {
const json = express.json(options);
return function (req, res, next) {
// enforce json body type. without this the json middleware will skip parsing and req.body might be undefined
if (_mime(req) !== 'application/json') return res.status(400).send('incorrect mime type. expecting application/json');
if (forceContentType && _mime(req) !== 'application/json') return res.status(400).send('incorrect mime type. expecting application/json');
json(req, res, next);
};
};