I am using https.request() to invoke and test a bunch of REST APIs. I need to maintain session information which invoking a bunch of APIs and so the cookies and other headers from one API response is to be submitted in the subsequent API request. That is, I want to grab the cookies from an API response and set them in another API request.
I tried using Bluecat library from npm but it doesn't work well with Promises. Are there any libraries out there which I can use to sequence API requests and maintain session throughout?
[update]
The following is a method that will return a promise after invoking a REST API.
const https = require('https');
function invokeREST_API(options){
let apiPromise = new Promise(function(fulfill, reject){
let req = https.request(options, (res) => {
let responseData = '';
res.on('data', (d) => {
responseData += d;
});
res.on('end', () => {
let response = JSON.parse(responseData);
if(response.statusCode == 0 ) {
fulfill(response);
}else {
reject(response);
}
});
});
});
return apiPromise;
}
This method will be called for each API in a flow and, they need to be executed in a sequence. For example, login -> getUserDetails -> saveUserDetails. Here I need to call login API, if the promise is resolved, proceed with calling getUserDetails API (with cookies from the previous API response) and so on.
How can I keep a track of cookies between API requests so that I can maintain session information throughout?