-
Notifications
You must be signed in to change notification settings - Fork 857
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue obtaining body and updating it as part of proxy #942
Comments
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 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));
}
},
})
); |
I think this can be closed as the docs now include
Commit: 09d05d5 |
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?
to proxy requests through to azure search. Specifically - ensure they are authenticated (via jwt token) with my middleware, - add the azure search APIKEY (removing the need to include it from the front end, protecting the key) - log the search request details - ideally (the issue here), modify the search based on the user's profile (from the jwt token)
What http-proxy-middleware configuration are you using?
don't understand the question
What OS/version and node/version are you seeing the problem?
Additional context (optional)
No response
The text was updated successfully, but these errors were encountered: