0

I have a login API call which response me a token. I have to use this token later for further API calls. I tried but can not manage how to save this token to var so I can use as a dynamic variable in the future API calls. The token experie in 10minutes, so time to time I have to login again and requets a new toke, this is why I need to be dynamic.

var token= "";

function login() {                

    
    const postData = `body information`;

    const options = {                                               
        protocol: 'https:',
        host: 'url',
        path: "/v2",
        method: 'POST',
        headers: {
            "Authorization": "Bearer " + '123',
    }
};

const req = https.request(options, (res) => {                     
    console.log('Login statusCode:', res.statusCode);

    res.on('data', (d) => {                                         
        answer += d;
        var token = answer      => This is where I would like to save the response to variable.
    });

    res.on("error", (err) => {                                      
        reject(err);
    });

});

req.write(postData);                                             

req.on('error', (e) => {                                         
    console.error(e);
});

req.end();

};



async function update() {                    
    
    await login();
    console.log(token)
};

update()

The exptected outcome is the token value from API response, but instead of it just show me empty.

Denes
  • 111
  • 7

0 Answers0