-
Notifications
You must be signed in to change notification settings - Fork 853
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
app.use(bodyParser.json()) breaks http-proxy-middleware for HTTP POST JSON with express #320
Comments
Maybe related #126 |
FYI I worked around this issue like so:
|
|
I instead excluded the proxied requests from the body parser middleware: https://stackoverflow.com/questions/27117337/exclude-route-from-express-middleware |
This is my solution: https://github.com/stuartZhang/coexist-parser-proxy |
You just need to reorder the middlewares;
|
Things like body-parser can break proxying anything with a body. This checks for that (req.body will become an Object) and streams the body into the proxy request. Fixes chimurai#90, chimurai#320, chimurai#417 - and maybe some more
Same with polka. |
Saved my soul |
Things like body-parser can break proxying anything with a body. This checks for that (req.body will become an Object) and streams the body into the proxy request. Fixes chimurai#90, chimurai#320, chimurai#417 - and maybe some more
Just a use case and slight variations on the work around. I'm using the Okta Middlware for Node library, which requires body parser, but then I want to include the user's access token in proxied api calls, according to the token relay pattern: The requires me to implement the aforementioned fix, but I don't see a requirement to define writeBody as a capture so mine looks like: // From poxy options
onProxyReq: (targetRequest: ClientRequest, clientRequest: Request) => {
// Headers must be set before doing anything to the body.
if (clientRequest?.userContext?.tokens?.access_token) {
targetRequest.setHeader('Authorization', 'Bearer ' + clientRequest.userContext.tokens.access_token);
}
fixBody(targetRequest, clientRequest);
}
/**
* http-proxy-middleware is not compatible with bodyparser and must appear before it without this fix.
* @see see https://github.com/chimurai/http-proxy-middleware/issues/320
* @param proxyReq
* @param req
*/
function fixBody(proxyReq: ClientRequest, req: Request): void {
if (!req.body || !Object.keys(req.body).length) {
return;
}
const contentType = proxyReq.getHeader('Content-Type') as string;
if (contentType.includes('application/json')) {
writeBody(proxyReq, JSON.stringify(req.body));
}
if (contentType === 'application/x-www-form-urlencoded') {
writeBody(proxyReq, querystring.stringify(req.body));
}
}
function writeBody(proxyReq: ClientRequest, bodyData: string) {
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
proxyReq.write(bodyData);
} |
For a test case of the following
curl -X POST --header 'Content-Type: application/json' --header 'Accept: text/html' -d '{ "$class": "org.accordproject.cicero.contract.AccordContractState", "stateId": "test2" }' 'http://domain/path'
Where http://domain/path uses http-proxy-middleware with express then the introduction of app.use(bodyParser.json()) for express with this http-proxy-middleware, all together the app.use(bodyParser.json()) breaks this.
This is an unexpected surprise and finding this cause of this error was difficult.
The text was updated successfully, but these errors were encountered: