Http-proxy-middleware: Proxying POST

Created on 8 Nov 2016  路  3Comments  路  Source: chimurai/http-proxy-middleware

I want to proxy my POST request. What i get is 301 redirection instead of 307 whenever i am doing a post request.

Thats my webpack-dev-server config:

gulp.task("serve", ['webpack', 'copyIndex', 'dump', 'vendor'], function (callback) {
function restream(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 proxy = {
    'localhost:8888/rest': {
      target: {
        host: 'my-app.pl',
        protocol: 'https',
        port: 80
      },
      changeOrigin: true,
      secure: false,
      logLevel: 'debug',
      onProxyReq: restream
    }
  };
  var myConfig = Object.create(webpackConfig);
  myConfig.devtool = "eval";
  myConfig.debug = true;
  myConfig.proxy = proxy;
  // Start a webpack-dev-server
  new WebpackDevServer(webpack(myConfig), {
    quiet: false,
    noInfo: false,
    contentBase: "./dist",
    proxy: proxy,
    stats: {
      colors: true
    }
  }).listen(8888, "localhost", function (err) {
    if (err) throw new gutil.PluginError("webpack-dev-server", err);
    gutil.log("[webpack-dev-server]", "http://localhost:8888/webpack-dev-server/index.html");
  });
});

I used this solution to restream parsed body, but req.body in restream function is always undefined, no matter if its GET request or POST with data.

GET requests are working fine. They are all proxied well.

Any ideas?

awaiting response

All 3 comments

You can try the following configuration options and see if those will fix the redirect issues.

hostRewrite: rewrites the location hostname on (201/301/302/307/308) redirects.
autoRewrite: rewrites the location host/port on (201/301/302/307/308) redirects based on requested host/port. Default: false.
protocolRewrite: rewrites the location protocol on (201/301/302/307/308) redirects to 'http' or 'https'. Default: null.

And how is the redirect issue related to restreaming?

Issue has gone stale; Closing issue.
Feel free to re-open.

Having the same problem, is there a solution?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

minhduchcm picture minhduchcm  路  7Comments

dzz9143 picture dzz9143  路  3Comments

kimmobrunfeldt picture kimmobrunfeldt  路  5Comments

villesau picture villesau  路  8Comments

udayms picture udayms  路  8Comments