You need to log in to a third-party server and access the content for authorized users.

PhantomJS is not suitable.

I tried this:

var request = require('request'); var cheerio = require('cheerio'); var credentials = { username: 'user', password: 'pass' }; request.post({ uri: 'http://mysite.ru/login', headers: { 'content–type': 'application/x–www–form–urlencoded' }, body: require('querystring').stringify(credentials) }, function(err, res, body){ if(err) { callback.call(null, new Error(' Login failed')); return; } request('http://mysite.ru/all', function(err, res, body) { if(err) { callback.call(null, new Error('Request failed')); return; } var $ = cheerio.load(body); console.log(body); }); }); 

But at the output gives the HTML code of the same login page.

I do not ask to edit my code, but maybe someone will stick his nose at the correct lesson? Preferably in conjunction with PostMan - ohm (so that you can analyze the standard browser requests and use them in your requests from the application)

    1 answer 1

    It turned out to do so:

     var request = require('request'); var cheerio = require('cheerio'); **var request = request.defaults({jar: true})** var credentials = { username: 'user', password: 'pass' }; request.post({ uri: 'http://mysite.ru/login', headers: { 'content-type': 'application/x-www-form-urlencoded' }, body: require('querystring').stringify(credentials), headers: {'user-agent': 'Mozilla/5.0'} }, function(err, res, body){ if(err) { callback.call(null, new Error(' Login failed')); return; } request('http://mysite.ru/all', function(err, res, body) { if(err) { callback.call(null, new Error('Request failed')); return; } var $ = cheerio.load(body); console.log(body); }); }); 

    But it is not clear how to manage all the data received (like session sessions and so on). If someone can help with this, help!)