puppeteer: response.buffer() rejects for selected resources

The promise returned by response.buffer() consistently rejects for certain resources. This makes it impossible to collect information about certain resources that can only be obtained from the buffer, such as the length of the resource.

The problem seems to particularly affect video/media files. (Perhaps because partial requests are involved?)

To reproduce:

'use strict';

const URL = 'https://www.quirksmode.org/html5/tests/video.html';

const puppeteer = require('puppeteer');

(async() => {

const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.on('response', response => {
  const url = response.url;
  if (!url.startsWith('data:') && response.ok) {
    response.buffer().then(
        b => {
          console.log(`${response.status} ${url} ${b.length} bytes`);
        },
        e => {
          console.error(`${response.status} ${url} failed: ${e}`);
        }
    );
  }
});

await page.goto(URL, { waitUntil: 'networkidle0' });

})();

Actual:

200 https://www.quirksmode.org/html5/tests/video.html 2905 bytes
200 https://www.quirksmode.org/quirksmode.css 16360 bytes
200 https://www.quirksmode.org/quirksmode.js 22540 bytes
200 https://www.quirksmode.org/pix/footer_bg.gif 312 bytes
206 https://www.quirksmode.org/html5/videos/big_buck_bunny.webm failed: Error: Protocol error (Network.getResponseBody): No data found for resource with given identifier undefined
206 https://www.quirksmode.org/html5/videos/big_buck_bunny.ogv failed: Error: Protocol error (Network.getResponseBody): No data found for resource with given identifier undefined
206 https://www.quirksmode.org/html5/videos/big_buck_bunny.webm 24804 bytes
206 https://www.quirksmode.org/html5/videos/big_buck_bunny.ogv 66642 bytes

Expected:

As above, but without the “Error: Protocol error (Network.getResponseBody): No data found for resource with given identifier undefined” messages.

The “broken” resources seem to be associated with 206 responses.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 2
  • Comments: 23 (8 by maintainers)

Most upvoted comments

Update reproduce code for Puppeteer 1:

'use strict';

const URL = 'https://www.quirksmode.org/html5/tests/video.html';

const puppeteer = require('puppeteer');

(async() => {

const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.on('response', response => {
  const url = response.url();
  if (!url.startsWith('data:') && response.ok) {
    response.buffer().then(
        b => {
          console.log(`${response.status()} ${url} ${b.length} bytes`);
        },
        e => {
          console.error(`${response.status()} ${url} failed: ${e}`);
        }
    );
  }
});

await page.goto(URL);

})();

In my case I was listening to the 'response' event when I should have been listening to the 'requestfinished' event.

@anaulin I was able to reproduce this with disabled JavaScript. It looks like server sometimes aborts the request - the video element on the page is non-functional indeed when this happens:

image

@aslushnikov you are absolutely right that i see the video blinking in again when i load that page – that’s a good catch, hadn’t noticed that before.

I also added that extra log line, and the error text seems to always be net::ERR_ABORTED:

=== ITERATION 1 ===
- loading URL
- video loading failed!
- errorText: net::ERR_ABORTED
- URL loaded.
- Browser closed.
=== ITERATION 2 ===
- loading URL
- fetching body of .mp4 file
-- .mp4: OK
- URL loaded.
- Browser closed.
=== ITERATION 3 ===
- loading URL
- fetching body of .mp4 file
-- .mp4: OK
- URL loaded.
- Browser closed.
=== ITERATION 4 ===
- loading URL
- video loading failed!
- errorText: net::ERR_ABORTED
- URL loaded.
- Browser closed.
=== ITERATION 5 ===
- loading URL
- fetching body of .mp4 file
-- .mp4: OK
- URL loaded.
- Browser closed.
=== ITERATION 6 ===
- loading URL
- video loading failed!
- errorText: net::ERR_ABORTED
- URL loaded.
- Browser closed.
=== ITERATION 7 ===
- loading URL
- video loading failed!
- errorText: net::ERR_ABORTED
- URL loaded.
- Browser closed.
=== ITERATION 8 ===
- loading URL
- video loading failed!
- errorText: net::ERR_ABORTED
- URL loaded.
- Browser closed.
=== ITERATION 9 ===
- loading URL
- video loading failed!
- errorText: net::ERR_ABORTED
- URL loaded.
- Browser closed.
=== ITERATION 10 ===
- loading URL
- video loading failed!
- errorText: net::ERR_ABORTED
- URL loaded.
- Browser closed.

Full script (for future reference):

const puppeteer = require('puppeteer');

const URL = 'https://www.nash.io/company/careers';
const VIDEO_URL =  'https://www.nash.io/static/careers-fe92d8a0bcbd10b63666cd11940a0f08.mp4';

(async() => {
  for (let i = 0; i < 10; ++i) {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();

    // This makes it work 100%.
    await page.setJavaScriptEnabled(false);

    page.on('requestfinished', async (request) => {
      if (request.url() !== VIDEO_URL)
        return;
      try {
        console.log('- fetching body of .mp4 file');
        await request.response().buffer();
        console.log(`-- .mp4: OK`);
      } catch (e) {
        console.log(`-- .mp4: FAIL - ${e.message}`);
      }
    });

    page.on('requestfailed', async (request) => {
      if (request.url() !== VIDEO_URL)
        return;
      console.log('- video loading failed!');
      console.log(`- errorText: ${request.failure().errorText}`);
    });

    console.log(`=== ITERATION ${i+1} ===`);
    console.log('- loading URL');
    await page.goto(URL, { waitUntil: 'networkidle0' })
    console.log('- URL loaded.');
    await browser.close()
    console.log('- Browser closed.');
  }
})();

@aslushnikov thanks for getting to the bottom of that – it is consistent with the net::ERR_ABORTED errors I was seeing. Looks like the issue for us is really this server breaking the connection, and not a Puppeteer issue after all.

Also experiencing this issue when intercepting a response whose .buffer() resolves to a pdf file.

I’m having the same issue, any update for this issue?

Then how to get the buffer of the 206 response?