mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2025-12-16 11:16:20 +00:00
refactor: use express method to check request content type
This commit is contained in:
committed by
Rainer Killinger
parent
60b689f28c
commit
3a7cc8d7c1
14
src/app.ts
14
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);
|
||||
|
||||
Reference in New Issue
Block a user