I know how to get the url, I couldn't find much on the page response text however.
await page.on('request', request => {
console.log('INTERCEPTED: ' + request.url());
request.continue();
});
How would I log the page response text?
I know how to get the url, I couldn't find much on the page response text however.
await page.on('request', request => {
console.log('INTERCEPTED: ' + request.url());
request.continue();
});
How would I log the page response text?
Share Improve this question asked Nov 11, 2020 at 16:52 LingeringDogSpongeLingeringDogSponge 671 silver badge7 bronze badges3 Answers
Reset to default 2You have to call setRequestInterception
before binding to request
await page.setRequestInterception(true);
page.on('response', (response) => {
console.log('RESPONSE RECEIVED');
console.log(response.status + ' ' + response.url);
});
page.on('request', request => {
console.log('INTERCEPTED: ' + request.url());
request.continue();
});
await page.setRequestInterception(true);
await page.on('requestfinished', async (request) => {
var response = await request.response();
try {
if (request.redirectChain().length === 0) {
var responseBody = await response.buffer();
console.log(responseBody.toString());
}
}catch (err) { console.log(err); }
});
await page.on('request', request => {
request.continue();
});
response.text() literally will always return null, its another redundant feature of puppeteer however the above works just fine to get the page response which apparently is only present when the request fully loads. ill never get over how many features of puppeteer literally seem to do nothing lol
You can find information about response.text()
here. It could be used like so:
page.on('response', async (response) => {
console.log(await response.text());
});
But the problem might also be with the termination of your script. The response you're looking for might not arrive before the end of your script. Imagine you have something like this:
await page.setRequestInterception(true);
page.on('request', request => {
request.continue();
});
page.on('response', response => {
if (response.url().includes('scripts'))
console.log(response.url());
});
await page.type('#search', 'foo');
await page.click('#send-search');
await context.close();
await browser.close();
then you can't be sure that a url with "scripts" will arrive before the end of your script. If it doesn't, you won't see anything in the console.
If you want to wait for a particular response, and do something with its text, it's better to type:
const res = await page.waitForResponse(response => response.url().includes('scripts'));
console.log(await res.text());