Entering a website and using the cookie data to enter the website again at a later time


#1

I am trying to save the cookie first time using HTTP.post authorize it with username and password. But when I am trying to use the same cookie for another link, I am unable to. when I use HTTP.get the cookie data is different.


#2

Can you show your code, in particular how you’re extracting the created cookie and passing it into the second HTTP.get request?


#3
var request = require('request');
var j = request.jar();
var main_page = "url1";
var url2 = 'url2';

request.get({url:main_page, jar: j, followAllRedirects: true}, function (err, res, body) {
	if (err) {
		return console.error('upload failed:', err);

	}
	//j.add(res.headers['set-cookie']);
	//console.log(cookieJar);
	console.log(request.cookie(main_page))
	console.log(res.headers['set-cookie']);
	request.post({
		url: url2,
		//jar: j,
		headers: res.headers,
		followAllRedirects: true,
		data: {username: '***',password: '***'}
},function(err,res1){
	if (!err){
		console.log (res.statusCode);
		console.log(res1.headers);
//console.log(res.cookie);
}
});
//console.log('Upload successful!  Server responded with:', body)
//console.log('jar\n'+ request.jar);
});

On meteor Server side.

When I check the cookie res.headers['set-cookie'] for the first and second url it is different.


#4

To start debugging try dropping the use of a custom cookie jar (so get rid of anything referencing j), then adjust your first line to be something like

var request = require('request').defaults({ jar: true });

to enable cookies and use the default cookie jar. That way we can at least rule that part out.


#5
var request = require('request').defaults({jar: true});
var main_page = "url1";
var url2 = 'url2';

request.get({url:main_page, followAllRedirects: true}, function (err, res, body) {
	if (err) {
		return console.error('upload failed:', err);

	}
	console.log(request.cookie(main_page))
	console.log(res.headers['set-cookie']);
	request.post({
		url: url2,
		followAllRedirects: true,
		data: {username: '***',password: '***'}
},function(err,res1){
	if (!err){
		console.log (res1.statusCode);
            console.log(res1.headers['set-cookie']);
}
});
});

Responded me with the Session id for the first GET request, status code: 200 OK and undefined. So is the cookie not set in the post session? Or am I checking it wrong?