简体   繁体   中英

Return json body in REQUEST nodejs

I'm using the request module to make an HTTP GET request to an url in order to get a JSON response.

However, my function is not returning the response's body.

Can someone please help me with this?

Here is my code:

router.get('/:id', function(req, res) {
  var body= getJson(req.params.id);
  res.send(body);
});

Here is my getJson function:

function getJson(myid){
  // Set the headers
  var headers = {
   'User-Agent':       'Super Agent/0.0.1',
   'Content-Type':     'application/x-www-form-urlencoded'
  }
  // Configure the request
  var options = {
    url: 'http://www.XXXXXX.com/api/get_product.php',
    method: 'GET',
    headers: headers,
    qs: {'id': myid}
  }

  // Start the request
  request(options, function (error, response, body) {
  if (!error && response.statusCode == 200) {
    return body;
  }
  else
    console.log(error);
  })
}
res.send(body); 

is being called before your getJson() function returns.

You can either pass a callback to getJson:

getJson(req.params.id, function(data) {
    res.json(data);
});

...and in the getjson function:

function getJson(myid, callback){
// Set the headers
var headers = {
'User-Agent':       'Super Agent/0.0.1',
'Content-Type':     'application/x-www-form-urlencoded'
}
// Configure the request
var options = {
url: 'http://www.XXXXXX.com/api/get_product.php',
method: 'GET',
headers: headers,
qs: {'id': myid}
}

// Start the request
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
    callback(body);
}
else
    console.log(error);
})  

}

or simply call:

res.json(getJson(req.params.id));

The problem is that you are doing a return, expecting that the router will get the content.

Since is an async callback, that will not work. You need to refactor your code to be async.

When you are doing return body; the function that is being returned is the callback of request, and in no part you are sending the body to the router.

Try this:

function getJson(myid, req, res) {
  var headers, options;

  // Set the headers
  headers = {
    'User-Agent':       'Super Agent/0.0.1',
    'Content-Type':     'application/x-www-form-urlencoded'
  }

  // Configure the request
  options = {
    url: 'http://www.XXXXXX.com/api/get_product.php',
    method: 'GET',
    headers: headers,
    qs: {'id': myid}
  }

  // Start the request
  request(options, function (error, response, body) {
    if (!error && response.statusCode == 200) {
      res.send(body);
    } else {
      console.log(error);
    }
  });
}

And this router:

router.get('/:id', function(req, res) {
  getJson(req.params.id, req, res);
});

Here, you are instead passing the res param to the getJson function, so the callback of request will be able to call it as soon as its able to do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM