【发布时间】:2021-06-10 06:15:13
【问题描述】:
我有这个功能:
function getCookies(response) {
console.log(response)
const raw = response.headers.raw()['set-cookie'];
return raw.map((entry) => {
const parts = entry.split(';');
const cookiePart = parts[0];
return cookiePart;
}).join(';');
}
const _curl = async ({method, URL, headers, body, redirectMode}) => {
let options = {
method: method,
redirect: redirectMode,
credentials: 'include',
headers: headers
}
body ? Object.assign(options, {body: body}) : null
return fetch(URL, ({...options}))
}
我正在向一些网站发送请求,而一些我需要从响应中提取响应和 cookie。
(async() => {
const getReferCredentials = await // EXTRACT ONLY THE COOKIES FROM THE "SET-COOKIE" HEADER
_curl({
URL: "https://website.com/",
method: "GET",
headers:{
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36",
},
body: false,
redirectMode: "manual"
})
const referCookies = getCookies(getReferCredentials);
cookies = referCookies
const getOnePage = await // EXTRACT RESPONSE BODY AND COOKIES FROM HEADER SET-COOKIE
_curl({
URL: "https://website.com/one-page/",
method: "GET",
headers:{
"coookie": cookies,
},
body: false,
redirectMode: "follow"
}).then((res) => res.text()).then((result) => $ = cheerio.load(result))
const token = $('meta[name=csrf-token]').attr('content');
const checkoutCookies = getCookies(getOnePage ); // Error
cookies = checkoutCookies
});
问题出在getOnePage 函数中,我无法从“SET-COOKIE”标头中提取cookie,同时我将变量$ 处理并声明为cheerio 模块的实例。我只能在删除 then 块/处理时提取 cookie,就像在函数 getReferCredentials 中一样。
我唯一需要做的就是从标题中提取cookie,从页面中提取html响应
【问题讨论】:
标签: javascript node.js async-await