Skip to content

Commit

Permalink
Merge 691f399 into 7ae82e8
Browse files Browse the repository at this point in the history
  • Loading branch information
sarumont committed Jan 12, 2021
2 parents 7ae82e8 + 691f399 commit a51088a
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/http-proxy-middleware.ts
@@ -1,4 +1,5 @@
import * as express from 'express';
import { ClientRequest } from 'http';
import * as httpProxy from 'http-proxy';
import * as _ from 'lodash';
import { createConfig } from './config-factory';
Expand Down Expand Up @@ -33,6 +34,9 @@ export class HttpProxyMiddleware {
// log errors for debug purpose
this.proxy.on('error', this.logError);

// fix proxied body if bodyParser is involved
this.proxy.on('proxyReq', this.fixBody);

// https://github.com/chimurai/http-proxy-middleware/issues/19
// expose function to upgrade externally
(this.middleware as any).upgrade = (req, socket, head) => {
Expand Down Expand Up @@ -174,4 +178,12 @@ export class HttpProxyMiddleware {

this.logger.error(errorMessage, req.url, hostname, target, err.code || err, errReference);
};

private fixBody = (proxyReq: ClientRequest, req: Request) => {
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 a51088a

Please sign in to comment.