http-proxy-middleware: POST request body is not proxied to the servers
I have in my backend defined:
var proxy = proxyMiddleware('/api', {
target: 'http://somehost.zero',
proxyTable: {
'/api/one': 'http://somehost.one',
'/api/two': 'http://somehost.two',
}
});
app.use(proxy);
and when I’m proxing POST request (lets say to the ‘/api/one’: ‘http://somehost.one’ ) I don’t know why but server http://somehost.one get request without body.
Did you had similar problem?
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 1
- Comments: 31 (4 by maintainers)
Links to this issue
Commits related to this issue
- Keep LibCal API creds secret All together now... * dotenv [1] * axios module [2] * proxy module [3] * serverMiddleware [4] * body-parser [5] Go team. Heavily based on modify-post example [6] from ... — committed to cul-it/signage by cappadona 6 years ago
- Mann reservable lcd (#73) * First go at study room availability for LCD Using two tiers of mocked data: * first result set is hardcoded into a mocked components * second result set is loadin... — committed to cul-it/signage by cappadona 6 years ago
- FIX: Proxying was not working for POSTs with bodies * http-proxy-middleware was not passing the POST body along - it was being dropped * There are lots of threads on this, but the most helpful was ... — committed to joshuatz/gcp-proxy-func by joshuatz 5 years ago
- refactor: Rm body parser from proxy The body parser was manipulating the request in transit which caused POST reqests with json to hang and never reach the server See this issue in http-proxy-middle... — committed to tkottke90/server-manager-v2 by tkottke90 4 years ago
- fix: proxy request stuck at loading Moved the body-parser middleware below the http-proxy-middleware A known issue causes unexpected behaviour if the `bodyparser` is loaded before `http-proxy-middlew... — committed to deshmukhmayur/apikey-auth-proxy by deshmukhmayur a year ago
- fix: proxy request stuck at loading Moved the body-parser middleware below the http-proxy-middleware A known issue causes unexpected behaviour if the `bodyparser` is loaded before `http-proxy-middlew... — committed to deshmukhmayur/apikey-auth-proxy by deshmukhmayur a year ago
- fix: proxy request stuck at loading Moved the body-parser middleware below the http-proxy-middleware A known issue causes unexpected behaviour if the `bodyparser` is loaded before `http-proxy-middlew... — committed to deshmukhmayur/apikey-auth-proxy by deshmukhmayur a year ago
- fix bug in post requets and put request https://github.com/chimurai/http-proxy-middleware/issues/40 https://stackoverflow.com/questions/25207333/socket-hang-up-error-with-nodejs/25651651\#25651651 — committed to rickyadriell/getting-started-with-microservices by rickyadriell a year ago
Did a little test and I am able to receive POST data at the
targetserver.I noticed you are using the
body-parsermiddleware: https://github.com/dejewi/proxy-bug/blob/master/app.js#L16You might want to change to order of the middlwares you are using. Try moving the
http-proxy-middlewareabove thebody-parserHope this solves the issue.
If you can’t change the order of the middleware; You can restream the parsed body before proxying the request. This should fix the POST request:
source: node-http-proxy/examples/middleware/bodyDecoder-middleware.js. (https://github.com/nodejitsu/node-http-proxy/pull/1027)
Here is example https://github.com/dejewi/proxy-bug
Is there any way for http-proxy-middleware to do a check for stuff like this in the future? I just spend 2 days ripping out my hair because I had absolutely zero debug info to go on? Just a proxy that did nothing until it timed out…
Apologies for resurrecting this, but anybody aware of another way to avoid this issue when you can’t change the order of middleware (e.g. if you’re adding proxies at runtime)?
Here https://github.com/chimurai/http-proxy-middleware/issues/40#issuecomment-420259997
And here https://github.com/chimurai/http-proxy-middleware/issues/177#issuecomment-364054609
And here https://github.com/chimurai/http-proxy-middleware/issues/150#issuecomment-284083246
And I just lost a few hours as well.
Just underscoring that if you see your GET requests being proxied just fine, but your POST requests seem to hang and not being proxied, this issue is providing solutions.
Using @chimurai’s method above to restream code, I’m hitting an error saying that I cannot set the headers because they have been sent to client already. Any idea why?
For now, I’m going to find a way to use bodyParser only on the specific routes which don’t use http-proxy-middleware. Thanks!
Still is for sure 😎
If you really cant rewrite the order, you can rebuild back the body, mind you this is in JSON format.
GRRR! Hi 😃 I arrived here from trying to figure out why webpack-dev-server was proxying a POST request and dropping the json body content somewhere on its way to the server. @chimurai 's fix ended up working, but I wasn’t even using bodyParser to start with. Are there any webpack-dev-server gurus that know any simpler solution than:
which all works, again, finally, but is there any easier way? I’m worried about others who might have to spend a lot of time tracking down that issue.
Spent a few hours trying to debug this same issue while setting up multiple proxies. The request would hang at the
target. MovingproxyMiddlewareabovebodyParsersolved my problem as well. Thank you!Hi, I’m seeing this issue too using browsersync.
My API does not appear to get the body:
If I remove
body-parsermiddleware thenreq.bodyis always undefined andrestreamfails because it has nothing to parse.6+ years later and this is still a lifesaver. Thank you!!