From 3a7cc8d7c16456973b565798f0c3b08279e0edd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jovan=20Kruni=C4=87?= Date: Mon, 28 Sep 2020 16:05:13 +0200 Subject: [PATCH] refactor: use express method to check request content type --- src/app.ts | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/src/app.ts b/src/app.ts index a0e5473f..164b117d 100644 --- a/src/app.ts +++ b/src/app.ts @@ -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);