jsoverson / puppeteer-interceptor

Easy request interception/response modification for puppeteer
51 stars 16 forks source link

Can't modify response headers #5

Open cqq626 opened 3 years ago

cqq626 commented 3 years ago

I want to modify response headers, but the codes below can't work.

intercept(page, patterns.Document('*'), {
    onInterception: event => {
      console.log(`${event.request.url} intercepted.`)
    },
    onResponseReceived: event => {
      event.response.headers.push({
        name: 'Link',
        value: '<https://x.xxx.com>; rel="preconnect"'
      });
      console.log(`${event.request.url} intercepted, going to modify`)
      return event.response;
    }
});

Besides, I try to modify the source code, use 'Fetch.continueRequest' to modify response headers, can't work either

// in my code
intercept(page, patterns.Document('*'), {
    onInterception: event => {
      console.log(`${event.request.url} intercepted.`)
    },
    onResponseReceived: event => {
      return null;
    }
});

// in source code
if (newResponse) {
    debug(`Fulfilling request ${requestId} with response returned from onResponseReceived`);
    console.log('newResponse headers count:', newResponse.headers.length);
    await client.send('Fetch.fulfillRequest', {
        requestId,
        responseCode: newResponse.statusCode,
        responseHeaders: newResponse.headers,
        body: newResponse.base64Body ? newResponse.base64Body : btoa_1.default(newResponse.body),
        responsePhrase: newResponse.statusMessage,
    });
}
else {
    // here I try to modify response headers
    await client.send('Fetch.continueRequest', { requestId, headers: [
        {name: 'Link', value: '<https://x.xxx.com>; rel="preconnect"'},
    ] });
}
cqq626 commented 3 years ago

I use puppeteer v5.5.0 and puppeteer-interceptor v2.2.0