I'm trying to enter a website with my credentials and download a pdf using puppeter. I got the pdf url using puppeteer, but now I want to use node-fetch to access that page. To fetch the pdf page I need to include the session data on options, but I don't know if I am doing it the right way.
I tried using 'credentials: 'include', getting the cookies with page.cookies and other small modifications in the options sent with the fetch.
var response = await page.goto(urlPdf);
var headersPup = response.request().headers();
const { cookies } = await page._client.send("Network.getAllCookies", {});
const sessionFreeCookies = cookies.map((cookie) => {
return {
...cookie,
expires: Date.now() / 1000 + 10 * 60,
session: false
};
});
headersPup['Cookie'] = sessionFreeCookies; //adding the cookies to header
headersPup['Content-Type'] = 'application/pdf';//adding content-type
var opts = {
method: "GET",
headers: headersPup,
credentials: "include",
}
await fetch(urlPdf,opts).then(response => response
.body.pipe(fs.createWriteStream('test4.pdf'))
.on('close', () => console.log('pdf downloaded')));
When I open test4 as txt I can see the login page html, it means I lost the session. How can I keep the session to download my pdf?