superluminar
Published in

superluminar

How to wait for and intercept a particular HTTP request in Puppeteer

Situation: In Puppeteer, one sometimes needs to wait for a particular HXR request (e.g., a particular URL has been called to happen) after a particular action (e.g., clicking a button), and you need the HTTP response object of that particular request.

Complication: There is page.waitForResponse but it only returns the values after e.g. Puppeteer.click has been called:

await page.click('some_identifier')// HXR request to wait for / intercept happens hereconst httpResponseWeWaitFor = await page.waitForResponse((response) => {
// it's already too late here to wait for / intercept the XHR request
return response.url().startsWith("https://somesample") // this signals to puppeteer that we found the response we waited for
});

Solution:

Use a promise to wait for the correct HTTP response (remove the await, do the action which fires the XHR (click in this example), and then solve the promise with await :

// don't wait for waitForResponse, but continue by omitting await
const httpResponseWeWaitForPromise = page.waitForResponse((response) => {
return response.url().startsWith("https://somesample")
});
// HXR request to wait for / intercept happens here
await page.click('some_identifier')
// wait for the Promise to get resolved
const httpResponseWeWait = await httpResponseWeWaitForPromise;

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Soenke Ruempler

Soenke Ruempler

co-founder superluminar. AWS, Cloud, Serverless, Wardley Maps, Toyota Kata. AWS Partner Ambassador and Community Builder. Co-Org ServerlessDays Hamburg.