-
Notifications
You must be signed in to change notification settings - Fork 234
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
Supporting proxy after request body has been read #469
Supporting proxy after request body has been read #469
Conversation
- Instead read the raw body to a promise ourselves and pass that for proxy - Need to use forked express-http-proxy because this problem has not been solved (merged) yet villadora/express-http-proxy#469 preventing passing self-parsed/buffered body for proxy.
@monkpow can we get this PR merged. This enabled me to write an elegant code instead of having to add a route for POST and PUT operations. |
this seems to match my use case as well!
but it looks like the empty stream is what's sent in the proxy request - if i set parseReqBody to true, then I get the issue mentioned here: #487 |
Thanks for this thoughtful fix and the appropriate tests. |
@monkpow, when will this fix be released? |
Hi @monkpow, @brandon-leapyear gentle reminder! |
(This is I'm not associated with this project, so I have no control over releasing this. But FYI |
@brandon-leapyear @Ekansh82 This is released in v2.1.0. Thanks for your long-suffering patience with this delay. |
✨ This is an old work account. Please reference @brandonchinn178 for all future communication ✨
Our exact use-case is a bit special:
{ strict: false }
for the JSON body parser, because our proxied endpoint can take in non-objects as inputbody-parser
functionsSo to implement this, we extracted out the raw body before
body-parser
can touch it, then for our proxy, we didto completely ignore the body that was parsed by
body-parser
/express-http-proxy
and manually set the body back to the raw body we got.But in our testing, we noticed that sending values that would be parsed as falsy JSON values, like
'false'
or'""'
, our tests would hang. Basically, it would get toexpress-http-proxy/lib/requestOptions.js
Lines 91 to 100 in 2aec188
and
req.body
would be falsy, so it would try to read the request body stream. But the stream had already been read, meaning thatgetRawBody
will never return.Just setting
parseReqBody
tofalse
didn't help either because it would skip the entire section that sends the result ofproxyReqBodyDecorator
and try to pipe the original request's body to the proxy request, which wouldn't pipe anything because the request body had already been read.express-http-proxy/app/steps/sendProxyRequest.js
Lines 40 to 68 in 2aec188
To fix the latter point, I'm checking
bodyContent
ifparseReqBody
isfalse
and sending that if it's populated. In normal operation,bodyContent
isnull
, but withproxyReqBodyDecorator
,bodyContent
is populated.To fix the hanging, I added an explicit check in
requestOptions
that makes sure the request hasn't already been read. This shouldn't happen in normal operation, but it would've saved me a lot of headache if it were there. Another option is to changeif (req.body)
toif (req.body !== undefined)
, but I figured that would be more change in behavior than this library might want.