From a36e08af876a1ed8f2d18828a282248683559b6a Mon Sep 17 00:00:00 2001 From: Marco Klein Date: Mon, 2 Oct 2023 00:47:15 +0200 Subject: [PATCH] fix: :bug: Fix body parsing and output --- src/app.ts | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/app.ts b/src/app.ts index ac1801a..3abee2d 100644 --- a/src/app.ts +++ b/src/app.ts @@ -43,17 +43,25 @@ export class App { //#region Private Methods private setupBodyParsing() { - this._webServer.use(function(req, _, next){ - var data = ""; - req.on('data', (chunk) => data += chunk) - req.on('end', () => { - (req as RequestWithContext).rawBody = data; - next(); - }); + this._webServer.use(async (req, res, next) => { + const promises = [ + new Promise(resolve => bodyParser.json()(req, res, resolve)), + new Promise(resolve => bodyParser.urlencoded({ extended: true })(req, res, resolve)), + new Promise(resolve => { + var data = ""; + + req.on('data', (chunk) => data += chunk) + req.on('end', () => { + (req as RequestWithContext).rawBody = data; + resolve(); + }); + }) + ]; + + await Promise.all(promises); + next(); }); - this._webServer.use(bodyParser.json()); - this._webServer.use(express.urlencoded({ extended: true })); } private setupKeyVerification(): void { @@ -149,7 +157,7 @@ export class App { this._logger.debug(`${prefix} HTTP Headers received: ${JSON.stringify(request.headers)}`); this._logger.debug(`${prefix} Query Parameters received: ${JSON.stringify(request.query)}`); this._logger.debug(`${prefix} Body received (RAW): ${request.rawBody}`); - this._logger.debug(`${prefix} Body received (RAW): ${request.body}`); + this._logger.debug(`${prefix} Body received: ${JSON.stringify(request.body)}`); response.on("finish", () => { this._logger.info(`${prefix} END with ${response.statusCode}`);