How can I fake browser POST request with NodeJS?

Viewed 1903

I want to send a POST request to example.com just like browser does. There's a form on example.com that you submit and it gives you the information for the subset you selected.

When I submit it with browser it shows the information. And here are the page headers in Chrome: Chrome headers

But when I try to send a request on my own from myserver.com like this:

request.post({
    url: 'http://example.com',
    json : false,
    withCredentials: true,
    headers: {
        //'POST /study/?id=11 HTTP/1.1':'',
        'Host':'example.com',
        'Connection':'keep-alive',
        'Content-Length':'34',
        'Cache-Control':'max-age=0',
        'Origin':'http://example.com',
        'Upgrade-Insecure-Requests':'1',
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36',
        'Content-Type':'application/x-www-form-urlencoded',
        'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Referer':'http://example.com',
        //'Accept-Encoding':'gzip, deflate',
        'Accept-Language':'en-US,en;q=0.8,ru;q=0.6',
        'Cookie':'ssd_num=1m99jeo61eb4srduha9osqr9p2',
        'X-Compress':'null',
    },
    formData: {
        'tmtblTip':'0',
        'showKla':'10%E0',
        'sbShRKl':'1'
    },
    encoding: null
}, (err, res, data) => {
    if (err) {
        console.log('Error:', err);
    } else if (res.statusCode !== 200) {
        console.log('Status:', res.statusCode);
    } else {
        // data is already parsed as JSON:
        //let bodyWithCorrectEncoding = iconv.decode(data, 'win1251');
        let bodyWithCorrectEncoding = toUtf(data, 'utf8','win1251');
        console.log(bodyWithCorrectEncoding);
    }
});

When I do this it returns the webpage, but it says There's no such subset.

Can you please tell me what is the difference between these requests (mine and browser)?

0 Answers
Related