伪造者中发帖请求的意外结果

时间:2019-01-12 08:46:54

标签: javascript node.js puppeteer

我知道这已经被另一个问题覆盖了: here。但是由于某种原因,我做错了。

我在需要数据的服务器上发帖时遇到问题。  here

如果我直接从网站的控制台(使用ctrl + shift + i)编写以下发布请求,则效果很好。

full = $.post('https://www.shufersal.co.il/_layouts/Shufersal_Pages/ajax.aspx',
{"AjaxCallAction":"AddProductToBasket","paramProductID":7296073231578,"paramQuantity":2,"paramRemarks":"","paramUM":""});

console.log(full.responseText); //required data

Here's我从服务器控制台获得的响应的屏幕截图: 在这里。

但是当我使用以下脚本编写此代码时,却没有得到相同的结果

const puppeteer = require('puppeteer');
async function run() {
    let browser = await puppeteer.launch({ headless: false });
    let page = await browser.newPage();
    await page.setRequestInterception(true); //set the request option (triggered with goto)
    page.on('request', req => {
        //create data
        let prodID = 7296073231578; //shoko
        let qty = 2;
        let remarks='';
        let unitofmeasure = '';
        var request = {
            AjaxCallAction:"AddProductToBasket",
            paramProductID:prodID,
            paramQuantity:qty,
            paramRemarks:remarks,
            paramUM:unitofmeasure
        }

        var data = {
            'method': 'POST',
            'url': 'https://www.shufersal.co.il/_layouts/Shufersal_Pages/ajax.aspx', //just to make sure
            'postData': JSON.stringify(request)
        };
        req.continue(data);

    });

    const response = await page.goto('https://www.shufersal.co.il/_layouts/Shufersal_Pages/ajax.aspx');
    console.log(response.statusText());
    console.log(response.responseText);

    console.log('done');
}   
run();

我在cmd中得到以下结果:

OK
undefined
done

此外,如果我尝试使用nodejs的“请求”发布请求: (使用answered question here) 但这似乎也不起作用。

1 个答案:

答案 0 :(得分:1)

例如,您需要使用x-www-form-urlencoded发送请求正文:

AjaxCallAction=AddProductToBasket&paramProductID=12345&....

修改请求正文或创建对象到urlencoded转换器。还需要设置标头Content-Type并捕获来自response事件侦听器的响应。

async function run() {
    let browser = await puppeteer.launch({headless: false});
    let page = await browser.newPage();
    await page.setRequestInterception(true); //set the request option (triggered with goto)
    page.on('request', req => {
        //create data
        let prodID = 7296073231578; //shoko
        let qty = 2;
        let remarks = '';
        let unitofmeasure = '';
        var request = {
            AjaxCallAction: "AddProductToBasket",
            paramProductID: prodID,
            paramQuantity: qty,
            paramRemarks: remarks,
            paramUM: unitofmeasure
        }
        // convert JSON to x-www-form-urlencoded
        let reqBody = Object.keys(request).map((k) => {
            return encodeURIComponent(k) + '=' + encodeURIComponent(request[k])
        }).join('&');
        var data = {
            'method': 'POST',
            'url': 'https://www.shufersal.co.il/_layouts/Shufersal_Pages/ajax.aspx',
             // the custom headers
            'headers': {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
                'X-Requested-With': 'XMLHttpRequest'
            },
            'postData': reqBody
        };
        req.continue(data);
    });
    // capture intercepted response
    page.on('response', async response => {
        console.log("Resource Type: "  + response.request().resourceType());
        console.log("Response Text: " + await response.text());
        console.log("==============");
    });

    const response = await page.goto('https://www.shufersal.co.il/_layouts/Shufersal_Pages/ajax.aspx');
    // only capture default response
    //console.log(response.statusText());
    //console.log(response.responseText);

    //console.log('done');

}

run();