-1

How can I get / pass the "body" from this function to a higher level (to another top level function)?

function f() {
    var request = require('request');
    request({
        method: 'GET',
        url: 'https://private-anon-147dec1d33-topupsapi.apiary-mock.com/operators/auto-detect/phone/+50936377111/country-code/HT?&includeBundles=true',
        headers: {
           'Accept': 'application/com.reloadly.topups-v1+json',
           'Authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik0wWXpRa'
        }
    }, function (error, response, body) {
        console.log('Status:', response.statusCode);
        console.log('Headers:', JSON.stringify(response.headers));
        console.log('Response:', body);
    });
}
trincot
  • 317,000
  • 35
  • 244
  • 286

1 Answers1

0
function req(callback){
 var request = require('request');

request({
method: 'GET',
url: 'https://private-anon-147dec1d33-topupsapi.apiary-mock.com/operators/auto-detect/phone/+50936377111/country-code/HT?&includeBundles=true',
headers: {
'Accept': 'application/com.reloadly.topups-v1+json',
'Authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Ik0wWXpRa'
}}, function (error, response, body) {
    callback(body)
});
}

function callback(body){
   // Your higher function
}

req(callback); 

One way is to pass body to a callback function.

djorborn
  • 151
  • 1
  • 7