I am doing a Rest call in nodejs to get a report from JasperSoft Server. And I need to get a cookie to stay connected but I don't know how to do get it
var http = require('http');
var options = {
host: '127.0.0.1',
port: 8080,
path: '/jasperserver/rest/login?j_username=jasperadmin&j_password=jasperadmin',
method: 'POST'
};
http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
}).end();
So it work but then I want to have acces to another link like :localhost:8080/jasperserver/ressource/reports And I need the cookie to do that. How can I do to get it ?
For your informations, console.log('HEADERS: ' + JSON.stringify(res.headers)); display the cookie and the path and some other thing, so maybe i just have to parse it and get the cookie from there but I don't know how to do.
Moreover, as I said I want to go to another link after being connected so can you also help me to set the cookie for another link ?
Look into using Mikeal Rogers' request module. It has built-in cookie handling, follows redirects, and other goodies. It's also a little simpler API than http.request
. Your cookies should just work after logging in.
Update: Sample with request
(npm install request
):
var request = require("request");
request.post({url: "http://localhost:8080/jasperserver/rest/login", qs: {j_username: "jasperadmin", j_password: "jasperadmin"}}, function(err, res, body) {
if(err) {
return console.error(err);
}
request.get("http://localhost:8080/jasperserver/ressource/reports", function(err, res, body) {
if(err) {
return console.error(err);
}
console.log("Got a response!", res);
console.log("Response body:", body);
});
});