refactor: use express method to check request content type

This commit is contained in:
Jovan Krunić
2020-09-28 16:05:13 +02:00
committed by Rainer Killinger
parent 60b689f28c
commit 3a7cc8d7c1

View File

@@ -93,20 +93,8 @@ export async function configureApp(app: Express) {
// only accept json as content type for all requests
app.use((req, res, next) => {
// get the content type
// TODO: Always lowercase (see type definition of IncomingHttpHeaders)
let contentType = '';
// the content type can be string, string[] or undefined
if (typeof req.headers['Content-Type'] === 'string') {
// weird type definitions require an explicit cast
contentType = req.headers['Content-Type'] as string;
} else if (typeof req.headers['content-type'] === 'string') {
// weird type definitions require no cast here though...
contentType = req.headers['content-type'];
}
// Only accept json as content type
if (contentType === '' || contentType.match(/^application\/json$/) === null) {
if (req.is('application/json') !== 'application/json') {
// return an error in the response
const err = new SCUnsupportedMediaTypeErrorResponse(isTestEnvironment);
res.status(err.statusCode);