I'm trying now since over 48 hours and googelt almost the whole web. My problem is that when use puppeteer the POST Request is not working - I tried many websites but the POST Form Action is not working. Can somebody help me?
File test.js Usage: node test.js
const puppeteer = require('puppeteer');
const randomUseragent = require('random-useragent');
const USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36';
(async () => { const browser = await puppeteer.launch({ args: ['--no-sandbox'] }) // headless: true,
const page = await browser.newPage()
await page.setViewport({
width: 1920 + Math.floor(Math.random() * 100),
height: 3000 + Math.floor(Math.random() * 100),
deviceScaleFactor: 1,
hasTouch: false,
isLandscape: false,
isMobile: false,
});
const userAgent = randomUseragent.getRandom();
const UA = userAgent || USER_AGENT;
await page.setUserAgent(UA);
await page.setJavaScriptEnabled(true);
await page.setDefaultNavigationTimeout(0);
await page.setRequestInterception(true);
page.on("request", interceptedRequest => {
var data = {
"method": "POST",
"postData": "URLz=VIDEOURL"
};
interceptedRequest.continue(data);
});
const response = await page.goto('https://fdown.net/download.php')
//const responseBody = await response.text();
await page.screenshot({ path: 'affe.png', fullPage: true })
await browser.close()
})()
First you need to be able to do manually from a browser, in the developper tab's console window:
// see https://stackoverflow.com/questions/6396101/pure-javascript-send-post-data-without-a-form
var xhr = new XMLHttpRequest();
xhr.open("POST", "https://fdown.net/download.php", true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function () {
if (this.readyState != 4) return;
if (this.status == 200) {
var data = this.responseText;
// we get the returned data
console.log(data)
}
else {
console.warn("POST error");
}
// end of state change: it can be after some time (async)
};
xhr.send("URLz=VIDEOURL");
and then you can make puppeteer do it in the page using page.evaluate which runs code in the target page:
const postResult = await page.evaluate(() => {
return new Promise((resolve, reject) => {
// see https://stackoverflow.com/questions/6396101/pure-javascript-send-post-data-without-a-form
var xhr = new XMLHttpRequest();
xhr.open("POST", "https://fdown.net/download.php", true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function () {
if (this.readyState != 4) return;
if (this.status == 200) {
var data = this.responseText;
// we get the returned data
console.log(data);
resolve(data);
}
else {
// error
reject(this)
}
// end of state change: it can be after some time (async)
};
xhr.send("URLz=VIDEOURL");
});
});