When i tried to log into one website using username and password through node.js; It always redirect to home page without redirecting to dashboard.
But when i tried same login via browser it shows following header.
This is the code i've used.
var request = require('request'),
cheerio = require('cheerio'),
querystring = require('querystring'),
cookieParser = require('cookie-parser'),
url = "https://www.revcontent.com/login";
// creating a clean jar
var j = request.jar();
var request = request.defaults({
jar: j,
followAllRedirects: true
});
var stringify = require('node-stringify');
//Login Credentials
var username = "xxxxxx";
var password = "xxxxxxx";
var url = "https://www.revcontent.com/login";
var url2 = "https://www.revcontent.com/widgets?filter_search=&tab=countries&devices=&country=&domain=test.com&start_date=2017-01-22&end_date=2017-04-22";
var cookie = request.cookie('revcontent.com');
j.setCookie(cookie, url);
request({url: url, jar: j}, function (error, response, body) {
if (!error) {
var $ = cheerio.load(body),
//Retriving CSRF Token
csrf = $("[name='rv_tsvm']").val();
console.log("Token = " + csrf);
console.log(j);
var form = {
login: 'Sign+In',
name: username,
password: password,
rv_tsvm: csrf,
};
var formData = querystring.stringify(form);
var contentLength = formData.length;
request({
headers: {
'Content-Length': contentLength,
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36',
'Content-Type' : 'application/x-www-form-urlencoded'
},
uri: url,
body: formData,
method: 'POST',
jar: j
}, function (err, res, body) {
console.log(res);
if(err) {
return console.error(err);
};
});
} else {
console.log("We’ve encountered an error: " + error);
}
});
via Mahesh

No comments:
Post a Comment