Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to intercept a download request on puppeteer and read the file being intercepted

I am using puppeteer for web scraping, i need to setup a request interception to read a file that is being downloaded from the browser without actually downloading it because it takes a lot of resources to download read and then delete it.

I have already identified the request, but found no way to read from it

await pages[0].setRequestInterception(true);
  pages[0].on('request', request => {
    if (request.resourceType() === 'font' || request.resourceType() === 'stylesheet' || request.resourceType() === 'image') {
      request.abort();
    } else {
      request.continue();
    }
 });
like image 711
srosati Avatar asked Oct 16 '22 12:10

srosati


1 Answers

I'd rather use the response event because the request interception doesn't have the response yet.

pages[0].on('response', async response => {
    if (response.request() /*Your condition check*/) {
      var buffer = await response.buffer(); /*You can get the buffer*/
      var content = await response.text(); /*You can get the content as text*/
    }
});
like image 68
hardkoded Avatar answered Oct 19 '22 12:10

hardkoded