Search code examples
node.jsexpressmean-stackmeanjs

Passing the data from the Express API Parameter to Request Module in MEAN Stack


I am facing below issue with passing the parameters from the Express API to Request module URL.

In the below code Suppose I have the request details as

request_data.url = http://localhost:3000/interface/en/

When users enters the URL as http://localhost:3000/interface/en/123456

I wanted to send the 123456 to the line

url: request_data.url + acct,

Hence my final url for the request module becomes as http://localhost:3000/interface/en/123456

But my below code is not working , can someone help me here or suggest me what changes are requires

Code

app.get('/interface/:env/:acct', (req, res) => {
    var acct = req.params.acct;
    var env = req.params.env;
    var hsResponse = request({
        proxy: proxyUrl,
        url: request_data.url + acct,
        headers: request_data.headers,
        method: request_data.method,
        form: oauth.authorize(request_data)
    }, function (error, response, body) {
        res.setHeader('Content-Type', 'application/json');
        res.send(body); //<-- send hsResponse response body back to your API consumer
    });
});

Solution

  • Please use following code,

                  app.get('/interface/:env/:acct', (req, res) => {
                      var acct = req.params.acct;
                      var env = req.params.env;
    
                      // here you need to update your url
                      request_data.url = request_data.url + acct;
    
                      var hsResponse = request({
                          proxy: proxyUrl,
                          url: request_data.url ,
                          headers: request_data.headers,
                          method: request_data.method,
                          form: oauth.authorize(request_data)
                      }, function (error, response, body) {
                          res.setHeader('Content-Type', 'application/json');
                          res.send(body); //<-- send hsResponse response body back to your API consumer
                      });
                  });
    

    I think you are using OAuth, where you passing form field to the request, which will need to authorize with existing mapped request_data like URL and other attributes.

    Hope this will help you !!