如何解决Node JS:如何在 Axios 上设置 Cookie
我在 NodeJs 中有一个脚本,它正在发送请求(来自 axios)以使用凭据进行身份验证,我正在尝试获取 cookie 并将其设置为在某个受保护的页面上发送另一个请求...
这是我的代码:
const axios = require('axios');
const https = require('https');
const http = require('http');
let request = axios.create({
withCredentials: true,httpsAgent: new http.Agent({
rejectUnauthorized: false,keepAlive: true,}),maxRedirects: 10,//cap the maximum content length we'll accept to 50MBs,just in case
maxContentLength: 50 * 1000 * 1000
})
const params = new URLSearchParams()
params.append('login','admin@gmail.com')
params.append('password','myAwesomeP@ssw0rd')
request({
method: 'post',url: 'http://some-url.com/login.PHP',data: params,headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},}).then(res => {
let cookies = res.headers['set-cookie']; // → give me ['PHPSESSID=sgetl6rd1edsvs8hu8qbnnoupa; path=/']
request.get("http://some-url.com/protected-content.PHP",{
headers: {
Cookie: cookies[cookies.length - 1],}
}).then(resp => {
console.log(resp.data); // → that gave me the login page content
})
}).catch(error => {
console.log(error)
});
您知道如何实现这一目标吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。