Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Puppeteer: Override API Requests and respond with mock data without another package

I am trying to get mock data for puppeteer for a dynamic vue application.

Here is what I have (with placeholder API urls):

await page.setRequestInterception(true)
    page.on('request', (request) => {
      console.log(request.url())
      if (request.url() === 'URL HERE') {
        request.respond({
          status: 200,
          content: 'application/json',
          body: JSON.stringify(MockData.image)
        })
      } else if (request.url() === 'URL HERE') {
        request.respond({
          status: 200,
          content: 'application/json',
          body: JSON.stringify(MockData.text)
        })
      } else {
        request.continue()
      }
    })

This snippet sits above all logic where the requests would come through, and the URLS are correct.

The mocked calls do not seem to finish or come through correctly. Is this a problem with my setup?

like image 566
Sean Avatar asked Dec 02 '25 04:12

Sean


1 Answers

Solution was to call launch with chromium security disabled.

browser = await puppeteer.launch({
  args: ['--disable-web-security']
})
like image 200
Sean Avatar answered Dec 04 '25 19:12

Sean