request-persistent
A simple module to add a toJSON
function and a fromJSON
function to cookie jars created with request.jar()
.
usage
var fs = require('fs');
var request('request');
require('request-persistent')(request);
var jar;
if (fs.existsSync('cookies.json')) {
jar = request.jar.fromJSON(fs.readFileSync('cookies.json'));
// can also be an object, request.jar.fromJSON(require('./cookies')) would work
} else {
jar = request.jar();
}
request({
jar: jar
method: 'POST',
url: 'http://website.com/login',
form: {
user: 'username',
pass: 'password'
},
}, function (err, resp, body) {
fs.writeFileSync('cookies.json', jar.toJSON());
});