http-proxy-middleware
http-proxy-middleware copied to clipboard
HttpProxyMiddleware [ERR_STREAM_WRITE_AFTER_END] when using agent
Checks
- [X] I understand project setup issues should be asked on StackOverflow or in GitHub Discussions.
- [X] I updated to latest
http-proxy-middleware
.
Describe the bug (be clear and concise)
I'm using HttpProxyMiddleware, with those options:
export const httpProxy = createProxyMiddleware({
agent: new HttpsProxyAgent('http://127.0.01:10701'),
pathRewrite: {
'/tob_gateway': '',
},
router: () => {
return 'http://URL_ADDRESSxxxxx';
},
secure: false,
changeOrigin: true,
on: {
proxyReq: (proxyReq, req) => {
const requestBody = (req as unknown as BodyParserLikeRequest)?.body;
if (!requestBody) {
return;
}
const writeBody = (bodyData: string) => {
proxyReq.write(bodyData);
};
const contentType = proxyReq.getHeader('Content-Type') as string;
if (
contentType &&
(contentType.includes('application/json') ||
contentType.includes('+json'))
) {
writeBody(JSON.stringify(requestBody));
}
if (
contentType &&
contentType.includes('application/x-www-form-urlencoded')
) {
writeBody(querystring.stringify(requestBody));
}
},
},
});
I know that the body is empty since I read the stream with express.raw which using bodyParser behind the scenes, but how can I write it back again before its sent? I cant make any request changes on 'onProxyReq', since I get errors like the above, and if I try to change any headers I get [ERR_HTTP_HEADERS_SENT]
I want to write the request body back before I send it to the server
Step-by-step reproduction instructions
1. ...
2. ...
Expected behavior (be clear and concise)
i can to write body when proxyReq event is triggered( use agent option)
How is http-proxy-middleware used in your project?
as a http proxy
What http-proxy-middleware configuration are you using?
export const httpProxy = createProxyMiddleware({
agent: new HttpsProxyAgent('http://127.0.01:10701'),
pathRewrite: {
'/tob_gateway': '',
},
router: () => {
return 'http://URL_ADDRESSxxxxx';
},
secure: false,
changeOrigin: true,
on: {
proxyReq: (proxyReq, req) => {
const requestBody = (req as unknown as BodyParserLikeRequest)?.body;
if (!requestBody) {
return;
}
const writeBody = (bodyData: string) => {
proxyReq.write(bodyData);
};
const contentType = proxyReq.getHeader('Content-Type') as string;
if (
contentType &&
(contentType.includes('application/json') ||
contentType.includes('+json'))
) {
writeBody(JSON.stringify(requestBody));
}
if (
contentType &&
contentType.includes('application/x-www-form-urlencoded')
) {
writeBody(querystring.stringify(requestBody));
}
},
},
});
What OS/version and node/version are you seeing the problem?
mac: 14.4.1
node: v20.15.0
Additional context (optional)
No response