http-headerspuppeteerhttp-head

Is it possible to make a HEAD request in puppeteer?


I have not been able to find documentation that addresses this. I want to grab header data for some links to text files to determine which are recent, but I don't want to download each file to find out this information.


Solution

  • So it turns out that I couldn't make a HEAD request directly from puppeteer, but I can use node's https library in conjunction with puppeteer to make a HEAD request. This very informative article helped me arrive at a solution.

    import https from 'https';
    import puppeteer from 'puppeteer';
    
    async function downloadWithLinks() {
        const browser = await puppeteer.launch({
            headless: true // false
        });
        const page = await browser.newPage();
        await page.goto(
            'https://unsplash.com/photos/tn57JI3CewI',
            { waitUntil: 'networkidle2' }
        );
        const imgUrl = await page.$eval('.tB6UZ', img => img.src);
    
        https.request(imgUrl, {method: 'HEAD'}, res=> {console.log(res.headers)}).end()
        browser.close()
    }