如何使用axios获取请求生成的cookie?
因此,当我在邮递员中发出 GET 请求时,在响应中会生成一个 cookie。我正在尝试使用 Node.js 中的 axios 来执行此请求。但是,我无法获取这个生成的 cookie。
我正在使用 axios 拦截器,例如:
axios.interceptors.response.use(function (response) {
console.log(response.headers);
return response;
}, function (error) {
return Promise.reject(error);
});
但是,它只返回:
{
'cache-control': 'private',
'content-type': 'text/html',
expires: 'Tue, 01 Mar 2022 19:53:24 GMT',
server: 'Microsoft-IIS/8.5',
'x-powered-by': 'ASP.NET',
date: 'Tue, 01 Mar 2022 19:53:23 GMT',
'content-length': '4067'
}
我像这样发出请求
var config = {
method: 'get',
url: 'example.com',
headers: {
'Cookie': 'ASPSESSIONIDAWSQAQSA=AKLJADOCJNNPPHOKGGMOPHJD'
}
};
axios(config)
.then(function (response) {
//console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
标头中还有更多内容。但我只是将其删除以使其不那么冗长。
在标头中,没有有关正在生成的 cookie 的信息。问题是我需要这个 cookie 来在网站中发出其他请求。
在请求中,我已经添加了一个 cookie,例如
{
'Cookie': 'ASPSESSIONIDAWSQAQSA=AKLJADOCJNNPPHOKGGMOPHJD',
'Connection': 'keep-alive'
}
但我需要在响应中生成的新 cookie。因此,我执行的下一个请求将有两个 cookie。
So, when I make a GET request in postman, in the response, one cookie was generated. I am trying to do this request with axios in Node.js. However, I am not able to get this generated cookie.
I am using axios interceptors like:
axios.interceptors.response.use(function (response) {
console.log(response.headers);
return response;
}, function (error) {
return Promise.reject(error);
});
But, it only returns:
{
'cache-control': 'private',
'content-type': 'text/html',
expires: 'Tue, 01 Mar 2022 19:53:24 GMT',
server: 'Microsoft-IIS/8.5',
'x-powered-by': 'ASP.NET',
date: 'Tue, 01 Mar 2022 19:53:23 GMT',
'content-length': '4067'
}
I make the request like this
var config = {
method: 'get',
url: 'example.com',
headers: {
'Cookie': 'ASPSESSIONIDAWSQAQSA=AKLJADOCJNNPPHOKGGMOPHJD'
}
};
axios(config)
.then(function (response) {
//console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
There are more things in the header. But I just took it out to make it less verbose.
In the headers, there is no information about the cookie that is being generated. The problem is that I need this cookie to make other requests in the website.
In the request I already add one cookie like
{
'Cookie': 'ASPSESSIONIDAWSQAQSA=AKLJADOCJNNPPHOKGGMOPHJD',
'Connection': 'keep-alive'
}
But I need the new cookie that is generated in the response. Therefore, the next request that I do will have two cookies.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论