From 691f399b7c0edae833181bc93db12062fde5fe5a Mon Sep 17 00:00:00 2001 From: Richard Kolkovich Date: Tue, 12 Jan 2021 13:46:00 -0700 Subject: [PATCH] fix: restrict this to JSON, write the new content-length It is possible that the content length is not equivalent due to JSON serialization, so we need to re-write the Content-Length header to the upstream. This commit also restricts the fix to ONLY handle JSON. Other content types could be handled with some conditionals if necessary. --- src/http-proxy-middleware.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/http-proxy-middleware.ts b/src/http-proxy-middleware.ts index ae7223c4..66fbf297 100644 --- a/src/http-proxy-middleware.ts +++ b/src/http-proxy-middleware.ts @@ -180,8 +180,10 @@ export class HttpProxyMiddleware { }; private fixBody = (proxyReq: ClientRequest, req: Request) => { - if (req.body instanceof Object) { - proxyReq.write(JSON.stringify(req.body)); + if (req.is('application/json')) { + const bodyData = JSON.stringify(req.body); + proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData)); + proxyReq.write(bodyData); } }; }