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: '.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);
})
}
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);
})
}
Share
Improve this question
edited Nov 3, 2014 at 20:37
chris-l
2,8411 gold badge19 silver badges19 bronze badges
asked Nov 3, 2014 at 19:52
Hirad RoshandelHirad Roshandel
2,1875 gold badges42 silver badges66 bronze badges
6
- and your get_product.php actually outputs JSON, right? – chris-l Commented Nov 3, 2014 at 19:55
- Have you tested this code against another similar service known to work? – tadman Commented Nov 3, 2014 at 19:55
- @chris-l when i write console.log(body) instead of return body; it shows the json data in my logs – Hirad Roshandel Commented Nov 3, 2014 at 19:56
- @tadman the URL works when I try it in my browser – Hirad Roshandel Commented Nov 3, 2014 at 20:00
- @HiradRoshandel Unless you're going to deploy your browser, you need to test your NodeJS code. What you have here should work. – tadman Commented Nov 3, 2014 at 20:05
2 Answers
Reset to default 9res.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.