-
Notifications
You must be signed in to change notification settings - Fork 854
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
POST request body is not proxied to the servers #40
Comments
I haven't tested the proxyTable option with POST requests yet. Which version of http-proxy-middleware are you using? Could you provide an example to show this problem? |
Hi, |
Here is example |
Thanks. How does your post request (url) look like? |
Hi, I'm seeing this issue too using browsersync. var proxy = proxyMiddleware('/api', {
target: 'https://xxx.xxx.xxx.xxx',
changeOrigin: true, // for vhosted sites, changes host header to match to target's host
secure: false
}); My API does not appear to get the body:
|
@chimurai it looks like: and in both cases data which I'm sending is: |
Sorry - do you mean this works for you or am I doing something wrong with my post request ? |
Did a little test and I am able to receive POST data at the I noticed you are using the You might want to change to order of the middlwares you are using. Hope this solves the issue. |
Yes, this solved my problem. Thank you very much for your help. |
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)? |
The same question :) |
I'm having this same (or a very similar) problem when using browser-sync with http-proxy-middleware. The middleware is moving my POST request to a GET request using x-www-form-urlencoded, which is being rejected by the server. |
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: // restream parsed body before proxying
var restream = function(proxyReq, req, res, options) {
if (req.body) {
let bodyData = JSON.stringify(req.body);
// incase if content-type is application/x-www-form-urlencoded -> we need to change to application/json
proxyReq.setHeader('Content-Type','application/json');
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
// stream the content
proxyReq.write(bodyData);
}
}
var apiProxy = proxyMiddleware('/api', {
target: 'https://xxx.xxx.xxx.xxx',
onProxyReq: restream
});
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(apiProxy); // with restreaming source: node-http-proxy/examples/middleware/bodyDecoder-middleware.js. (http-party/node-http-proxy#1027) |
Spent a few hours trying to debug this same issue while setting up multiple proxies. The request would hang at the |
For my purpose, I was able to make this work by changing the order i.e. putting body-parser after proxy-middleware.. |
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. |
It seems to work now with Webpack 3.6.0 - I tried removing my fix after I had an issue trying to post JSON.stringify(true), and the proxy no longer chokes on the json body, no matter the shape! |
I ran into a similar issue where the proxied post parameters weren't being included. The issue was that the request headers didn't specify application/json.
It is strange that isn't any error being specified or logged and that the post body is just ignored. |
@chimurai why ? This solved my problem ,but why |
@chimurai becuase it changes the req.body property |
@chping2125 Not sure about @aryeharmon answer, but as I understand, the request body data is a node stream (if you remember express tutorials, the See this here https://github.com/expressjs/body-parser/blob/master/lib/read.js#L162 That is why chimurai said on his post (#40 (comment)) that his middleware patch "restreams" the body data, not to the original This kind of transforms the data 2 times, but its an example solution that he proposed (and doesn't handles if was parsed from Hope it's clearer now. |
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! |
Here #40 (comment)
And here #177 (comment)
And here #150 (comment)
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. |
* 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 here: chimurai/http-proxy-middleware#40 (comment) * Followed advice and recipe (https://github.com/http-party/node-http-proxy/blob/master/examples/middleware/bodyDecoder-middleware.js) to parse body and then re-stream in request * Tested and working with POST and even application/x-www-form-urlencoded
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-middleware's github: chimurai/http-proxy-middleware#40
Hi all, If anyone is still looking at this, this worked for me too. For those new to express/node, if you use the express
I got it working by putting it directly after the |
If I remove |
Thank you |
6+ years later and this is still a lifesaver. Thank you!! |
If you really cant rewrite the order, you can rebuild back the body, mind you this is in JSON format.
|
Moved the body-parser middleware below the http-proxy-middleware A known issue causes unexpected behaviour if the `bodyparser` is loaded before `http-proxy-middleware`. Ref: chimurai/http-proxy-middleware#40 (comment)
Moved the body-parser middleware below the http-proxy-middleware A known issue causes unexpected behaviour if the `bodyparser` is loaded before `http-proxy-middleware`. Ref: chimurai/http-proxy-middleware#40 (comment)
Moved the body-parser middleware below the http-proxy-middleware A known issue causes unexpected behaviour if the `bodyparser` is loaded before `http-proxy-middleware`. Ref: chimurai/http-proxy-middleware#40 (comment)
Still is for sure 😎 |
in version 3 you can just doing something like this, const { createProxyMiddleware, fixRequestBody } = require('http-proxy-middleware');
const proxy = createProxyMiddleware({
/**
* Fix bodyParser
**/
on: {
proxyReq: fixRequestBody,
},
}); |
was struggling with the POST request part since the last few days |
Thanks for sharing |
I ran into this issue with request body urlencoded form data (and header Content-Type application/x-www-form-urlencoded). The origin protocol is HTTPS, and target HTTP, so I thought that might be the reason. I changed the request body to JSON (and header Content-Type application/json), and use express.json(), then the body was passed on to the target :-) So maybe request body type JSON is to prefer, if possible... :-) Cheers |
I have in my backend defined:
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?
The text was updated successfully, but these errors were encountered: