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

Multipart upload reset connection

Open GiuliaBusnelli opened this issue 7 years ago • 5 comments

I need to proxy a multipart upload request from a web application to an API server. The target server checks the file size and if it is bigger than expected return a 413 - Payload Too Large with a message.

If I send the request directly to the API server, I get the expected 413. But if I send the request to the proxy, the connection is reset.

Setup

  • http-proxy-middleware: 0.17.4
  • server: express v4.16.2

Code

Here is my code

// Set request timeout
var setTimeout = function(req, res, next) {
  req.setTimeout(30*60*1000);
  next();
}

// Set up API proxy
var options = {
  logLevel: process.env.LOG_LEVEL || 'info',
  target: process.env.PROXY_TARGET,
  pathRewrite: {
    '^/webapp/api' : '/api'
  },
  onProxyReq: function(proxyReq, req, res) {
    // Add username header to request
    proxyReq.setHeader('X-DevID', req.session[ cas.session_name ]);
  }
}
var apiProxy = proxy(options)

app.use('/graph4u-webapp/api', setTimeout, apiProxy);
var server = app.listen(process.env.PORT)

If I add the following snippet of code as an option

onProxyRes: function(proxyRes, req, res) {
  proxyRes.on('data', function(data) {
    console.log(data.toString('utf-8'));
  });
}

this is what is printed:

{
  "httpStatusValue":413,
  "errorName":"PAYLOAD_TOO_LARGE",
  "message":"La dimensione del file caricato (14MB) supera quella massima consentita (10MB).",
  "url":"..."
}

So it seems to me that the proxy receives the expected response form the API server but then it does not forward it correctly.

Can someone please help me fix this issue? Let me know if I must add more information and feel free to ask questions.

GiuliaBusnelli avatar May 17 '18 09:05 GiuliaBusnelli

Not sure what's happening.

Providing some extra information on the target server and its configuration might help.

Even better would be to provide a minimal setup in which this issue can be reproduced.

chimurai avatar May 17 '18 20:05 chimurai

I meet the same quetion too, when i use the proxy ,the chrome show net::ERR_CONNECTION_RESET, when without proxy, chrome shows 413 (Request Entity Too Large)

qianlongdoit avatar Nov 27 '18 03:11 qianlongdoit

Think this is an issue for the upstream library http-proxy.

Try creating a issue on their issue tracker: https://github.com/nodejitsu/node-http-proxy/issues

To help out investigation; Is it reproducible with just the http-proxy library? You can use a their example: https://github.com/nodejitsu/node-http-proxy/blob/master/examples/http/reverse-proxy.js

chimurai avatar Nov 27 '18 20:11 chimurai

I looked at this issue node-http-proxy. Found it quite similar to this issue. Try this

onProxyReq: (proxyReq, req, res) => {
        const contentType = proxyReq.getHeader('Content-Type');
        let bodyData;

        if (contentType === 'application/json') {
            bodyData = JSON.stringify(req.body);
        }

        if (contentType === 'application/x-www-form-urlencoded') {
            bodyData = queryString.stringify(req.body);
        }

        if (bodyData) {
            proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
            proxyReq.write(bodyData);
        }
}

ConsidNdr avatar Nov 20 '20 13:11 ConsidNdr

@ConsidNdr Thank you soo much. It is working for me..

SivaKrishnaKola avatar Aug 12 '21 02:08 SivaKrishnaKola