I'd like to use requestjs to make requests and share the cookies between multiple javascript files and even multiple servers. For that reason I'm storing the cookies in a database as a string and retrieve them when needed.
The string looks like this which should be compliant to tough-cookie
[{"domain":"domain.com","path":"/","secure":true,"expires":"2018-06-19T15:36:04.000Z","key":"key1","value":"val1","httpOnly":false,"hostOnly":true},{"domain":"domain.com","path":"/","secure":true,"expires":"2018-06-19T15:36:04.000Z","key":"key2","value":"val2","httpOnly":false,"hostOnly":true}]
How can I get requestjs to use those cookies?
The tough-cookie
object has a method fromJSON(string)
which I suppose is what I need.
So supposedly I think I should be able to do
var cookies = '[{"domain":"domain.com","path":"/","secure":true,"expires":"2018-06-19T15:36:04.000Z","key":"key1","value":"val1","httpOnly":false,"hostOnly":true},{"domain":"domain.com","path":"/","secure":true,"expires":"2018-06-19T15:36:04.000Z","key":"key2","value":"val2","httpOnly":false,"hostOnly":true}]';
var j = request.jar();
j.fromJSON(cookies);
request.get({ url: 'https:/url.com', jar : j},
function(error, response, body) {
console.log(body);
});
but this gives the error TypeError: j.fromJSON is not a function
How can I get request to use the cookies string fetched from the database?