0

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?

GnyG
  • 151
  • 1
  • 2
  • 11
  • Usually what is used to maintain cookies so that your session stays alive is called a "cookie jar". There are a number of NPM modules that offer that functionality. Sequencing requests is a completely different issue. If you show us exactly what you're trying to do (with code) we can help more specifically on that topic. – jfriend00 Jun 21 '17 at 23:24
  • Thanks for the reply @jfriend00. I have updated the post with some code snippets and details. – GnyG Jun 24 '17 at 00:25
  • Did you look up how to use a cookie jar with the [`request()` module](https://github.com/request/request) to maintain a session from one request to the next? It's all right there with code examples. As for the rest of your question, i can't really tell what else you're asking. There are thousands of articles and hundreds of answers here on stack overflow (many of them authored by me) on sequencing asynchronous tasks using promises. I'd suggest you read those and then ask a more specific question about where you got stuck. – jfriend00 Jun 24 '17 at 00:35

0 Answers0