Cannot proxy after parsing body
See original GitHub issueI cannot re-send a proxied request’s body after I have parsed it.
Using this recipe, this comment and this comment I have written the following example:
import express from 'express';
import proxy from 'http-proxy-middleware';
import bodyParser from 'body-parser'
const app = express();
app.use(bodyParser.json());
app.post('*', proxy({
target: 'http://target.here.com',
secure: false,
onProxyReq: (proxyReq, req) => {
if (req.body) {
const bodyData = JSON.stringify(req.body);
proxyReq.setHeader('Content-Type','application/json');
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
proxyReq.write(bodyData);
}
}
}));
app.listen(2001, () => console.log('Listening'));
I get an error when trying to set the first header on the proxyReq
saying: Cannot set headers after they are sent to the client
.
If that is the case how does the recipe work?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:7
- Comments:9 (1 by maintainers)
Top Results From Across the Web
Not getting response using an express proxy server when ...
I am facing a problem where I am not getting a request back when I use this in one of the servers :...
Read more >Unable to parse response body - Opster
If your client is getting the response from the reverse proxy service this could be what is causing the “Unable to parse response...
Read more >The message body is not a valid JSON. Unexpected character ...
ApiManagement. Proxy: The message body is not a valid JSON. Unexpected character encountered while parsing value. - Microsoft Q&A.
Read more >RFC 7230: Hypertext Transfer Protocol (HTTP/1.1)
One consequence of this flexibility is that the protocol cannot be ... a header field-value after message parsing has delineated the individual fields....
Read more >5.x API - Express.js
This parser accepts any Unicode encoding of the body and supports automatic ... Since router and app implement the middleware interface, you can...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
+1 got the same issue. Moving the middlewares before body.json works
If need to parse the body before proxy, I have followed suggestion from this issue: https://github.com/chimurai/http-proxy-middleware/issues/177#issuecomment-420423934