I have a project that requires the use of requests and is using javascript run by node. I am having trouble storing the body of a request to a global variable so I can access it in other functions. Is there a way to save the response in a global variable? Thanks.
var request = require('request');
var globalBody = "";
var options = {
url: 'http://www.google.com/',
headers: {
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_4 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10B350 Safari/8536.25'
}
}
request(options, function (error, response, body) {
if(error != null){
console.log('error:', error);
}
if(response.statusCode != 200){
console.log('statusCode:', response && response.statusCode);
}else{
globalBody = body;
}
});
console.log(globalBody)
The last line "console.log(globalBody)" results in "" but I want it to display the body of the request. Is there any way to do this?
via Noah Cover
No comments:
Post a Comment