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

how to pass new parameters to initial request

Open victorbucutea opened this issue 8 years ago • 5 comments

How to do this as in decorateProxyReqOpts I have the following code which seems to remove all params:

return Promise .resolve(resolverFn(container.proxy.reqBuilder, container.user.req)) .then(function(processedReqOpts) { delete processedReqOpts.params; container.proxy.reqBuilder = processedReqOpts; return Promise.resolve(container); });

I haven't even seen this documented

victorbucutea avatar Jul 17 '17 14:07 victorbucutea

req.params contains route parameters (in the path portion of the URL), and req.query contains the URL query parameters (after the ? in the URL).

Are you saying you'd like to pass route parameters to the proxy request? I'm confused about the request.

If you are instead asking about query parameters you'd use a proxyReqResolver to add new query params to the request. If you are asking about req body (maybe form parameters) you'd use proxyReqBodyDecorator.

HTH.

monkpow avatar Jul 21 '17 13:07 monkpow

@monkpow I'm having a related (I think) issue... I'm trying to use the proxyReqBodyDecorator you mentioned but the value is not actually getting added to the request body that goes out to the proxy..

i.e. I'm sending this payload via postman: {test: true}

I'm then using this body decorator:

const bodyDecorator = function(proxyReq, srcReq) {
    return new Promise(res => {
      ctrl.getOrgLocationsKeys(srcReq)
        .then(locsApiKeys => {
          let newContent = Object.assign({}, srcReq.body, {apiKeys: locsApiKeys});
          console.log('BODY CONTENT: ', newContent); //prints out exactly what I'd expect...
          res(newContent);
        });
    });
  };

And here's the usage of the proxy:

router.use('/:tablePayCode', proxy(config.oloAPI.url, {
    proxyReqPathResolver: req => {
      console.log(`REQ URL: ${req.url}`);
      return `${config.tablePay.url}/${req.params.tablePayCode}`;
    },
    proxyReqBodyDecorator: bodyDecorator
  }));

But then when I dump the request within the API that I'm proxying to, it only gets the original request body and not the compiled one from the decorator... It's not clear to me what's wrong here - any thoughts?

bradleygore avatar Dec 04 '17 17:12 bradleygore

proxyReqResolver is not documented, has it been deprecated? it seems that only doing:

  app.all(path, function (req, res) {
    req.query.newAddedParam = 'abcde';
    apiProxy.web(req, res, { target: target });
  });

Won't work, the newAddedParam is not sent to the target.

What's the ideal way to solve this?

valecarlos avatar Mar 21 '19 16:03 valecarlos

@valecarlos did you find any solution ?

magic-mohit avatar Jan 08 '21 10:01 magic-mohit

@magic-mohit @valecarlos Unless I'm misunderstanding you, proxyReqPathResolver allows you to operate on the query params before the request is sent to the proxy server. It is documented here https://github.com/villadora/express-http-proxy#proxyreqpathresolver-supports-promises, and I've added a new test to demonstrate more explicitly (#256).

Let me know if you have any questions. I'm going to close this ticket; feel free to re-open if you want to continue the discussion.

monkpow avatar Jan 12 '21 15:01 monkpow