使用Axios在Javascript中复制登录后请求
我正在尝试将登录后请求复制到此网站: 我正在使用带有Node.js的axios客户端。但是,当我在请求之后检索响应的数据时,它不会给我account页面。 这是我的密码:使用Axios在Javascript中复制登录后请求,javascript,http,authentication,post,axios,Javascript,Http,Authentication,Post,Axios,我正在尝试将登录后请求复制到此网站: 我正在使用带有Node.js的axios客户端。但是,当我在请求之后检索响应的数据时,它不会给我account页面。 这是我的密码: const fs = require('fs') const axios = require('axios') const cheerio = require('cheerio'); const qs = require('qs') const session = axios.create({ baseURL: &qu
const fs = require('fs')
const axios = require('axios')
const cheerio = require('cheerio');
const qs = require('qs')
const session = axios.create({
baseURL: "https://shop.adidas.com.sa/en/customer/account/login/",
withCredentials: true
})
async function getFormKey() {
try {
const response = await session.get('https://shop.adidas.com.sa/en/customer/account/login');
const $ = cheerio.load(response.data);
let form_key = $('[name=form_key]').attr('value')
return form_key
} catch (error) {
console.error(error);
}
}
const loginUser = async (email, password) => {
let form_key = await getFormKey();
let data = qs.stringify({
form_key: form_key,
login: {
username: email,
password: password
},
})
let options = {
headers: {
'origin': "https://shop.adidas.com.sa/",
'referer': "https://shop.adidas.com.sa/en/customer/account/login/",
'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Mobile Safari/537.36'
},
}
session.post("https://shop.adidas.com.sa/en/customer/account/loginPost/", data, options)
.then(function (response) {
fs.writeFile('test.txt', response.data, err => {
if (err) {
console.error(err)
return
}
//file written successfully
})
})
.catch(function (error) {
console.log(error);
});
}
代码看起来不错,如果你没有账户页面,你会得到什么?有错误吗?没有,我没有收到任何错误,如果我检索response.data,我只会收到登录页面