Node.js Puppeteer: Image Download Returns Access Denied Error

Using Puppeteer in Node.js, fetching an image via a custom file stream triggers an ‘Access Denied’ error, likely due to CORS/referrer policies.

const secureHttp = require('https');
const fileSystem = require('fs');

function fetchPhoto(photoLink, destination, onDone) {
  const outputStream = fileSystem.createWriteStream(destination);
  secureHttp.get(photoLink, (response) => {
    response.pipe(outputStream);
    outputStream.on('finish', () => {
      outputStream.close();
      onDone();
    });
  }).on('error', (err) => {
    console.error('Failed to retrieve image:', err);
  });
}

module.exports = { fetchPhoto };

I experienced a similar issue when downloading images using Puppeteer. In my case, the error was due to insufficient HTTP headers that made the request look unusual to the server. I resolved it by ensuring that the request mimicked one from a regular browser session, setting headers like user-agent and referrer explicitly before initiating the download. This approach helped bypass the server’s security protocols and prevented the ‘Access Denied’ error. Relying on Puppeteer’s capabilities to simulate real browser behavior proved effective for handling these restrictions.

The solution that worked for me involved leveraging Puppeteer’s ability to simulate real browser requests more accurately. I incorporated page.setExtraHTTPHeaders to pass typical browser headers before making the request, including user agent and referrer. This approach convinced the server that the request was coming from a normal browsing session rather than a programmatic download. I also adjusted my device emulation settings on Puppeteer to further mimic standard client behavior, which completely resolved the ‘Access Denied’ issue I was encountering.