Open coupster74 opened 10 months ago
maybe you can use the body-parser before use http-proxy-middleware
All.. regarding this issue, I found most of the guidance difficult to follow.. there was no single example which just worked. I did manage to figure it out though, and recommend it be added as an example in the documentation. For the code example below, I was proxying azure search and needed to do two things: 1) add the apikey (as an authenticated REST call, this ensured only authenticated users could call search, and keep the APIKEY secret from the client); 2) change the parameters of the search (in the body) based on the user's contest.
import * as httpProxy from 'http-proxy-middleware';
import * as bodyParser from 'body-parser';
/* snip */
app.use('/search/proxy', async (req: Request, res, next) => {
bodyParser.json()(req, res, (err) => {
if (err) return next(err);
// get required context here.. (in my case, user context)
if (req.body) {
// make body changes here
}
httpProxy.createProxyMiddleware({
target: process.env.AZURE_SEARCH,
changeOrigin: true,
pathRewrite: {
'^/search/proxy': '',
},
onError: (err, req, res) => {
res.writeHead(500, {
'Content-Type': 'text/plain',
});
res.end('Something went wrong...' + err);
},
onProxyReq: async (proxyReq, req: Request) => {
// Update the content length for the proxy request cause i may have changed the body
proxyReq.setHeader(
'Content-Length',
Buffer.byteLength(JSON.stringify(req.body)),
);
// add the search API key
proxyReq.setHeader('api-key', process.env.AZURE_SEARCH_APIKEY);
// Write out the body data
//console.log(JSON.stringify(req.body));
proxyReq.write(JSON.stringify(req.body));
},
})(req, res, next);
} catch (err) {
console.log(err);
return next(new InternalServerErrorException());
}
});
});
I had a related issue forwarding post, put and delete requests to a NextJS API. Your answer pointed me in direction of the solution which was to add proxyReq.write(JSON.stringify(req.body));
in the onProxyReq
callback. Took me several hours to solve so I will leave my solution here for future generations:
app.use(
"*",
createProxyMiddleware({
target: targetUrl,
changeOrigin: true,
onProxyReq: (proxyReq, req) => {
if (req.method !== "GET" && Object.keys(req.body).length > 0) {
proxyReq.write(JSON.stringify(req.body));
}
},
})
);
Checks
http-proxy-middleware
.Describe the bug (be clear and concise)
I am proxying azure search queries, and would like to obtain the complete request body, modify it, and forward it on. I've tried a number of things, and the results are consistently where either the body is empty, or by the time I get the entire body, the request has already been sent through to the server and cannot be changed.
I've reviewed the recipe here: https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/modify-post.md, (which just replaces the body, a unrealistic usecase), but I don't quite understand why we assume we would have the body at all since it is a stream. I've had to add this within the
onProxyReq:
to obtain the body:here is the code I currently have which obtains the body successfully, but it is too late to modify it.
what am I missing here?
Step-by-step reproduction instructions
Expected behavior (be clear and concise)
Within the
onProxyReq
there would be an approach to obtain, modify and forward on the req.body successfullyHow is http-proxy-middleware used in your project?
What http-proxy-middleware configuration are you using?
What OS/version and node/version are you seeing the problem?
Additional context (optional)
No response