Skip to content

Commit

Permalink
fix: restrict this to JSON, write the new content-length
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sarumont committed Jan 12, 2021
1 parent a6ee3fd commit 691f399
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/http-proxy-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
}

0 comments on commit 691f399

Please sign in to comment.