ChromeDevTools / devtools-protocol

Chrome DevTools Protocol
https://chromedevtools.github.io/devtools-protocol/
BSD 3-Clause "New" or "Revised" License
1.15k stars 226 forks source link

Can't get body on intercepted Document responses #153

Closed jsoverson closed 4 years ago

jsoverson commented 5 years ago

I'm encountering an issue where intercepted requests of type Document at the "HeadersReceived" stage are throwing the error "Can only get response body on requests captured after headers received" when trying to retrieve the response body. The same code works fine on Script and Image resources

Reproducible test case (via puppeteer):

const puppeteer = require('puppeteer');

(async function main(){
  const browser = await puppeteer.launch({
    headless:false, 
    defaultViewport:null,
  });

  const page = (await browser.pages())[0];

  const client = await page.target().createCDPSession();

  await client.send('Network.enable');

  await client.send('Network.setRequestInterception', { 
    patterns: [
      { urlPattern: '*', resourceType: 'Document', interceptionStage: 'HeadersReceived' }
    ]
  });

  client.on('Network.requestIntercepted', async ({ interceptionId, request, responseHeaders, resourceType }) => {
    console.log(`Intercepted ${request.url} {interception id: ${interceptionId}}`);

    const response = await client.send('Network.getResponseBodyForInterception',{ interceptionId });

    console.log(`Continuing interception ${interceptionId}`)

    client.send('Network.continueInterceptedRequest', {
      interceptionId,
    });
  });

  page.goto('https://google.com');

})()

Expected: Chromium pops up and loads google

Actual: Get an error in terminal

$ node min-repro-case.js
Intercepted https://google.com/ {interception id: interception-job-1.0}
(node:7557) UnhandledPromiseRejectionWarning: Error: Protocol error (Network.getResponseBodyForInterception): Can only get response body on requests captured after headers received.
    at Promise (/Users/joverson/development/src/tutorials/rev-eng-1-pup-method/node_modules/puppeteer/lib/Connection.js:183:56)
    at new Promise (<anonymous>)
    at CDPSession.send (/Users/joverson/development/src/tutorials/rev-eng-1-pup-method/node_modules/puppeteer/lib/Connection.js:182:12)
    at CDPSession.client.on (/Users/joverson/development/src/tutorials/rev-eng-1-pup-method/min-repro-case.js:24:35)
    at CDPSession.emit (events.js:182:13)
    at CDPSession._onMessage (/Users/joverson/development/src/tutorials/rev-eng-1-pup-method/node_modules/puppeteer/lib/Connection.js:200:12)
    at Connection._onMessage (/Users/joverson/development/src/tutorials/rev-eng-1-pup-method/node_modules/puppeteer/lib/Connection.js:112:17)
    at WebSocketTransport._ws.addEventListener.event (/Users/joverson/development/src/tutorials/rev-eng-1-pup-method/node_modules/puppeteer/lib/WebSocketTransport.js:41:24)
    at WebSocket.onMessage (/Users/joverson/development/src/tutorials/rev-eng-1-pup-method/node_modules/ws/lib/event-target.js:120:16)
    at WebSocket.emit (events.js:182:13)
(node:7557) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:7557) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Versions:

$ npm list | grep puppeteer
├─┬ puppeteer@1.12.2
$ ./node_modules/puppeteer/.local-chromium/mac-624492/chrome-mac/Chromium.app/Contents/MacOS/Chromium --version
Chromium 73.0.3679.0
TimvdLippe commented 4 years ago

This repository is related to Chrome DevTools Protocol, but does not track issues regarding its definition or implementation. If you want to file an issue for the Chrome DevTools Protocol, please open an issue on https://crbug.com under component: Platform>DevTools>Platform. Thanks in advance!