3
rated 0 times
[
8]
[
5]
/ answers: 1 / hits: 34849
/ 10 Years ago, mon, november 3, 2014, 12:00:00
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);
})
}
More From » json