Skip to content
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

Proxying POST #127

Closed
lukaszpochrzest opened this issue Nov 8, 2016 · 3 comments
Closed

Proxying POST #127

lukaszpochrzest opened this issue Nov 8, 2016 · 3 comments

Comments

@lukaszpochrzest
Copy link

lukaszpochrzest commented Nov 8, 2016

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?

@chimurai
Copy link
Owner

chimurai commented Nov 8, 2016

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?

@chimurai
Copy link
Owner

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

@vtni
Copy link

vtni commented Mar 12, 2019

Having the same problem, is there a solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants