I'm trying to use request to make API calls to a Fortigate device. The login method is to do a POST against /logincheck
with the credentials and then use the response cookies for subsequent API calls.
That part works fine and I do get the cookies. However, I can't see the Cookie header when I make the second call and the cookieJar looks empty.
Here's the code:
const config = require('./config/config');
const request = require('request');
var url = `${config.fortigate.adminURL}/logincheck`;
var cookieJar = request.jar();
request.post(
{
url : url,
headers : {
"Accept": "application/json"
},
form: {
username: config.fortigate.user,
secretkey: config.fortigate.password
},
jar: cookieJar
},
function (error, response, body) {
url = `${config.fortigate.adminURL}/api/v2/monitor/router/ipv4/`;
request({
url : url,
headers : {
"Accept": "application/json"
},
jar: cookieJar
},
function (error, response, body) {
console.log(response.request.headers);
console.log(response.statusCode);
console.log(cookieJar);
});
}
);
Output of the console.log commands below:
Headers: { Accept: 'application/json' }
Status code: 401
RequestJar {
_jar:
CookieJar {
enableLooseMode: true,
store: { idx: { '192.168.1.99': { '/': {} } } } } }
I've read the manual here https://github.com/request/request but still can't get it to work.
Also found another post which does have a solution, but other people also has issues with it: How to maintain a request session in NodeJS
Surely I have missed something, but what? Any ideas?
Update
The cookies expires 1969. Maybe the cookie jar does not persist in memory cookies between requests?
Cookie="APSCOOKIE_2739400828="0%260"; Expires=Fri, 09 May 1969 12:47:54 GMT; Path=/; Secure; SameSite=Strict; hostOnly=true; aAge=6ms; cAge=6ms"