问题描述
我在 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 (将#修改为@)