I want to inject axios in puppeteer in order to open a browser session, but sending requests and receiving responses through axios:
So the question is, is it possible? Here’s my attempt, but it is not working since axios doesn’t intercepts the website responses
JavaScript
x
12
12
1
const axios = require('axios');
2
const puppeteer = require ('puppeteer');
3
4
(async () =>{
5
const browser = await puppeteer.launch({headless:false})
6
const page = await browser.newPage()
7
await page.goto('https://InstanceOfUrl.com/something.php')
8
const res = await axios.get('https://InstanceOfUrl.com/something.php?action=examples/foo')
9
const data = res.data
10
console.log(data)
11
})()
12
I’m moving my first steps in axios, so I beg you to be patient; Thanks for your help in advance
Advertisement
Answer
I encourage you to go into a more verbose version using Promises:
- combining with @Simon’s advise using
fetch
JavaScript
1
11
11
1
puppeteer
2
.launch({headless:false})
3
.then(browser => browser.newPage())
4
.then(page => page.goto('https://InstanceOfUrl.com/something.php'))
5
.then(() => fetch('https://InstanceOfUrl.com/something.php?action=examples/foo')
6
.then(data => console.log(data))
7
.catch(error => {
8
throw new Error(error.message);
9
})
10
.finally(() => browser.close());
11
This will allow you to reason about each object passed into the next .then
in the promise chain.
Hope it helps