http-proxy-middleware icon indicating copy to clipboard operation
http-proxy-middleware copied to clipboard

[ERROR] - JSON POST BODY

Open prajwalkshetty opened this issue 6 years ago • 5 comments

Is this a question?

When using it with express server and trying to post json body, it seems like anything above 1KB since request fails. I tried using express.json() middle also body parser but it does not seem to go along smoothly with the proxy middleware. Any help regarding this issue would be helpful. Thanks in advance for the time.

this.app .use(this.parseRequestBody.bind(this)) .use('*', apiProxy) .use(express.json()) .listen(proxyPort);

Is this a bug report?

Yes

Is this a feature request?

No

  • http-proxy-middleware: 1.0.4
  • http-proxy-middleware configuration { // This value is not used but this is mandatory. target: proxyPort.toString(), changeOrigin: true, followRedirects: true, preserveHeaderKeyCase: true, pathRewrite: this.rewritePath.bind(this), router: this.rewriteHost.bind(this), onError: function onError(err, _req, _res) { // TODO: In future expose it as a callback. }, onProxyReq: this.onProxyReq.bind(this) }
  • server: express
  • other relevant modules

(Paste the link to an example project and exact instructions to reproduce the issue.)

prajwalkshetty avatar Jul 11 '20 04:07 prajwalkshetty

Can't really do anything without minimal reproducible example.

chimurai avatar Jul 14 '20 11:07 chimurai

@chimurai Sorry forgot to update here. I was able to figure out a fix for the issue from my side. Thanks for taking time to review the issue.

prajwalkshetty avatar Jul 14 '20 12:07 prajwalkshetty

Would be nice if you could share your solution for developers running into the same issue.

chimurai avatar Jul 14 '20 18:07 chimurai

I tried this from https://github.com/chimurai/http-proxy-middleware/issues/320#issuecomment-600470764 and it worked for me.

You just need to reorder the middlewares; put bodyParser after all proxy middlewares and routes without proxy after bodyParser ex:

app.use('/proxy/routex', myProxy);
app.use('/proxy/routey', myProxy);

... //then, after all proxys
app.use(bodyParser.json());

//and now, all no proxy routes
app.use('/api', (req,res)=>{
//TODO
});

app.get('/api/test', (req,res)=>{
//TODO
});

geoangelotti avatar Jul 16 '20 19:07 geoangelotti

@chimurai @geoangelotti Sorry for the delay in responding. Back i already had in that order even after that it was causing me issues, for me it was a specific issue of post body being greater than 1KB. For some reason something was adding a header i had to delete below headers before passing the request object to the proxy middleware and everything worked fine after that. delete req.headers['expect'] delete req.rawHeaders['expect']

prajwalkshetty avatar Jul 16 '20 20:07 prajwalkshetty