Senči
Senči

Reputation: 941

How to deal with Sessions in Node.js?

I'm trying to use a Website within node.js. Unfortunately the Site is telling me to enable the save of Session-Cookies. I tried to define a cookie-jar but i had no luck with it.

Simplified version of the code causing the problem:

var request = require('request');
var j = request.jar();
request.defaults({jar: j});
request({
    "uri": "https://www.somesite.tld",
    "method": "GET"
},function(error, response, body) {
    request({
        "uri": "https://www.somesite.tld/login",
        "method": "POST",
        "form": {
            "user": "Username",
            "pwd": "password"
        }
    }, function (error, response, body) {
        console.log(body);
    });
});

Upvotes: 0

Views: 420

Answers (1)

mscdex
mscdex

Reputation: 106736

You have to add your cookies to the cookie jar (at some point) before making the request. See this answer for a similar example.

Upvotes: 1

Related Questions