我正在尝试在节点js中使用http发送hhtp Web请求,但无法在函数内部获取变量
示例代码如下
const https = require('https')
const options = {
hostname: 'accounts.spotify.com',
path: '/login/?continue=https%3A//www.spotify.com/in/account/overview/&_locale=en-IN',
method: 'GET'
}
var csrf = 'rs';
const req = https.request(options, (res) => {
console.log(`statusCode: ${res.statusCode}`);
// const setCookie = req.getHeader('set-cookie');
console.log(res);
console.log(res['headers']['set-cookie'][0]);
csrf_token = res['headers']['set-cookie'][0];
csrf = csrf_token.match("csrf_token=(.*);Version");
csrf = csrf[1];
console.log(csrf); // Here it shows correct csrf
})
req.on('error', (error) => {
console.error(error)
})
req.end()
console.log(csrf); // here it shows rf as csrf which not desired
在req.end之后的最后一行,当我尝试执行日志csrf时,我得到csrf作为rf。我无法找到确切的方法来获取函数之外的变量
我要在下一个发布请求中使用此get请求中的csrf。