I am trying to invoke a rest API URL in one method and then use that response to form the next query to another URL, let's say git to query some information. I have seen numerous examples before posting this, in everyone people are simply printing the response to console or setting it to the server response.
I have tried using async/await but it just makes the whole thing really slow. So, I am not so keen on using that approach.
I am doing the callbacks but I am not able to collect data to actually loop over it. Here is my code snippet:-
function initialize(path) {
// Setting URL and headers for request
var options = {
url: '',
headers: {
'Accept': '*/*',
'User-Agent' : 'something something',
'Authorization': 'Basic ' + new Buffer(process.env.username + ':' + process.env.password).toString('base64')
},
method: 'GET',
port:443
};
options.url = path;
callback = function(err,response){
var str= "";
response.on('data',function(chunk){
str+=chunk;
});
response.on('end',function(){
return JSON.parse(str);
});
}
http.request(options, callback).end();
}
And I call this from this method.
function collectData1(userid){
var url = "example.com/users/user1";
var results = initialize(url);
console.log(results);
return results;
}
But results always stays undefined.
So, how do I do this? Any suggestions?