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

how to pass new parameters to initial request #256

Open
victorbucutea opened this issue Jul 17, 2017 · 5 comments
Open

how to pass new parameters to initial request #256

victorbucutea opened this issue Jul 17, 2017 · 5 comments
Labels

Comments

@victorbucutea
Copy link

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

@monkpow
Copy link
Collaborator

monkpow commented Jul 21, 2017

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.

@bradleygore
Copy link

@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?

@valecarlos
Copy link

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?

@magic-mohit
Copy link

@valecarlos did you find any solution ?

@monkpow
Copy link
Collaborator

monkpow commented Jan 12, 2021

@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.

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

No branches or pull requests

5 participants