Optionally allow json middleware to not check the content-type
This commit is contained in:
@@ -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);
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user