forked from chimurai/http-proxy-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix-request-body.ts
29 lines (24 loc) · 888 Bytes
/
fix-request-body.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import type * as http from 'http';
import type { Request } from '../types';
import * as querystring from 'querystring';
/**
* Fix proxied body if bodyParser is involved.
*/
export function fixRequestBody(proxyReq: http.ClientRequest, req: http.IncomingMessage): void {
const requestBody = (req as Request).body;
if (!requestBody) {
return;
}
const contentType = proxyReq.getHeader('Content-Type') as string;
const writeBody = (bodyData: string) => {
// deepcode ignore ContentLengthInCode: bodyParser fix
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
proxyReq.write(bodyData);
};
if (contentType && contentType.includes('application/json')) {
writeBody(JSON.stringify(requestBody));
}
if (contentType && contentType.includes('application/x-www-form-urlencoded')) {
writeBody(querystring.stringify(requestBody));
}
}