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

http-proxy form data encoded problem. #2424

Closed
feitian124 opened this Issue Nov 1, 2014 · 7 comments

Comments

Projects
None yet
4 participants
@feitian124

feitian124 commented Nov 1, 2014

i was using xdomain to solve cors problems then switched to --proxy option and both work well.

but when i try http-proxy with http-mock, the http-proxy not send correct data to rails.
it may be my config problem or http-proxy problem, i had submitted a issue in http-proxy.

I resubmit here in case someone meet same problem and hoping someone here can help.


I am doing ember dev and use http-proxy to proxy request from ember to server.
the code is as below.

  app.use('/oauth/token', function(req, res, next){

     // include root path in proxied request
    req.url = path.join(proxyPath, req.url);

    console.log('proxy req header:', req.headers);
    console.log('proxy req body:', req.body);

    proxy.web(req, res, {
      target: 'http://127.0.0.1:3000'
    });
  });
};

the req header and body seems ok from the log output:

proxy req header: { host: '127.0.0.1:4200',
  connection: 'keep-alive',
  'content-length': '55',
  accept: 'application/json, text/javascript, */*; q=0.01',
  origin: 'http://127.0.0.1:4200',
  'x-requested-with': 'XMLHttpRequest',
  'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36',
  'content-type': 'application/x-www-form-urlencoded',
  referer: 'http://127.0.0.1:4200/login',
  'accept-encoding': 'gzip,deflate,sdch',
  'accept-language': 'en-US,en;q=0.8,zh-CN;q=0.6' }

proxy req body: { grant_type: 'password',
  username: 'username',
  password: 'password' }

but after proxy, the rails get params as below:

  Parameters: {"{\"grant_type\":\"password\",\"username\":\"username\",\"passwor"=>"[FILTERED]"}

the correct params should like

Parameters: {"grant_type"=>"password", "username"=>"username", "password"=>"[FILTERED]"}

it seems the proxy parsed the form data in url in a "error" way that rails treat the params before "passwor" as a entire string. BTW, why "password" lost a "d" ...

could someone help? thanks.

@feitian124

This comment has been minimized.

Show comment
Hide comment
@feitian124

feitian124 Nov 1, 2014

by the way, i tried not use http-mock with http-proxy but http-mock with --proxy, then the --proxy seems not work . i don't know if they can work together, --proxy required another module 'connect-proxy', it works fine alone..

feitian124 commented Nov 1, 2014

by the way, i tried not use http-mock with http-proxy but http-mock with --proxy, then the --proxy seems not work . i don't know if they can work together, --proxy required another module 'connect-proxy', it works fine alone..

@feitian124

This comment has been minimized.

Show comment
Hide comment
@feitian124

feitian124 Nov 3, 2014

in the generated server/index.js file, i uncommented bodyParser and connect-restreamer together, then verything is fine. I'm using ember-cli 0.46.

if new version have the same code pattern, i guess still have problem.

feitian124 commented Nov 3, 2014

in the generated server/index.js file, i uncommented bodyParser and connect-restreamer together, then verything is fine. I'm using ember-cli 0.46.

if new version have the same code pattern, i guess still have problem.

@ahacking

This comment has been minimized.

Show comment
Hide comment
@ahacking

ahacking Nov 6, 2014

@feitian124 I had to do something similar to get proxy to work with http-proxy and form-data encoding (used for file upload).

Since I don't have any http-mocks currently I just moved the proxy config to the top. My index.js looks like this (with some some extra console logging):

module.exports = function(app) {
  var bodyParser = require('body-parser');
  var globSync   = require('glob').sync;
  var mocks      = globSync('./mocks/**/*.js', { cwd: __dirname }).map(require);
  var proxies    = globSync('./proxies/**/*.js', { cwd: __dirname }).map(require);

  // put this first for now
  proxies.forEach(function(route) { route(app); });

  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({
    extended: true
  }));

  mocks.forEach(function(route) { route(app); });

  // proxy expects a stream, but express will have turned
  // the request stream into an object because bodyParser
  // has run. We have to convert it back to stream:
  // https://github.com/nodejitsu/node-http-proxy/issues/180
  app.use(require('connect-restreamer')());
};

ahacking commented Nov 6, 2014

@feitian124 I had to do something similar to get proxy to work with http-proxy and form-data encoding (used for file upload).

Since I don't have any http-mocks currently I just moved the proxy config to the top. My index.js looks like this (with some some extra console logging):

module.exports = function(app) {
  var bodyParser = require('body-parser');
  var globSync   = require('glob').sync;
  var mocks      = globSync('./mocks/**/*.js', { cwd: __dirname }).map(require);
  var proxies    = globSync('./proxies/**/*.js', { cwd: __dirname }).map(require);

  // put this first for now
  proxies.forEach(function(route) { route(app); });

  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({
    extended: true
  }));

  mocks.forEach(function(route) { route(app); });

  // proxy expects a stream, but express will have turned
  // the request stream into an object because bodyParser
  // has run. We have to convert it back to stream:
  // https://github.com/nodejitsu/node-http-proxy/issues/180
  app.use(require('connect-restreamer')());
};
@ahacking

This comment has been minimized.

Show comment
Hide comment
@ahacking

ahacking Nov 6, 2014

I'm not particularly interested in becoming a node expert as I prefer other server technologies, so there may be a nicer way to handle this that would work for everyone out of the box.

ahacking commented Nov 6, 2014

I'm not particularly interested in becoming a node expert as I prefer other server technologies, so there may be a nicer way to handle this that would work for everyone out of the box.

@stefanpenner

This comment has been minimized.

Show comment
Hide comment
@stefanpenner

stefanpenner Nov 18, 2014

Contributor

@ahacking yes I don't want to subject anyone to having to learn node if they don't have to. I'll try to get around to ensure this works out of the box.

Contributor

stefanpenner commented Nov 18, 2014

@ahacking yes I don't want to subject anyone to having to learn node if they don't have to. I'll try to get around to ensure this works out of the box.

@ahacking

This comment has been minimized.

Show comment
Hide comment
@ahacking

ahacking Nov 18, 2014

@stefanpenner thanks, at least my work around above was enough for the time being.

ahacking commented Nov 18, 2014

@stefanpenner thanks, at least my work around above was enough for the time being.

@chadhietala

This comment has been minimized.

Show comment
Hide comment
@chadhietala

chadhietala Jan 23, 2015

Member

@ahacking @feitian124 was this resolved in #2568?

Member

chadhietala commented Jan 23, 2015

@ahacking @feitian124 was this resolved in #2568?

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