0

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()

})()
Gumigo
  • 11
  • 2
  • 1
    Why are you sending that string in postData? Shouldn't you be sending a json? – Manuel Duarte Jan 20 '22 at 20:07
  • How can I do that? Sending in json? – Gumigo Jan 20 '22 at 20:27
  • What do you mean by "not working", specifically? What are you trying to do and what effect/behavior is leading you to believe it's not working? Thanks for clarifying. – ggorlen Jan 20 '22 at 21:30
  • I don’t pass the POST Data to the website - I tried everything also with other sites but the POST data is not getting to the site – Gumigo Jan 20 '22 at 22:02

1 Answers1

1

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");
    });
});
lezhumain
  • 387
  • 2
  • 8