Passing variable from one request to another


  • Module Developer

    Hi,
    Hopefully a simple question.

    I am trying to pull data for my heating system, which requires two requests be made. The first request gets the sessionID, and the second logs in using the sessionID and pulls back the JSON data.

    I can see the SessionID in the first request, but how do I have this value available to the second request?

    Thanks



  • @mongo116 after the first request you can store the sessionid as a module property and then you can easily access it for your second request. or if you need the sessionid just once you can create another request right after getting the response where you can access the sessionid


  • Module Developer

    Thanks for the reply. Sorry to be a pain, but any chance of you putting a simple example of both on here.



  • @mongo116 if you post how you get your session id we can try to help you better


  • Module Developer

    This is from node_helper.js:

    	request({
    		url: "https://api-prod.bgchprod.info:443/omnia/auth/sessions",
    		headers: {
    				'Content-Type': 'application/vnd.alertme.zoo-6.1+json',
    				'Accept': 'application/vnd.alertme.zoo-6.1+json',
    				'X-Omnia-Client': 'Hive Web Dashboard',
    				'cache-control': "no-cache",
    				 },
    		method: "POST",
    		body: JSON.stringify(body),
    	}, function (error, response, body) {
    		if (!error && response.statusCode == 200) {
    			var responseJson = JSON.parse(body);
    		}
    	});
    	
    	request({
    		url: "https://api-prod.bgchprod.info:443/omnia/nodes",
    		headers: {
    				'Content-Type': 'application/vnd.alertme.zoo-6.1+json',
    				'Accept': 'application/vnd.alertme.zoo-6.1+json',
    				'X-Omnia-Client': 'Hive Web Dashboard',
    				'cache-control': "no-cache",
    				'X-Omnia-Access-Token': ,
    				 },
    		method: "GET",
    	}, function (error, response, body) {
    		if (!error && response.statusCode == 200) {
    			var responseJson = JSON.parse(body);
    		}
    	});
    

    The Session ID is part of the JSON response from the first request (responseJson.sessions[0].sessionId). Session ID needs to be put into the ‘X-Omnia-Access-Token’: , of the second request.

    Thanks



  • @mongo116 I assume that your code is inside the function getData

    getData: function() {
      request({
        url: "https://api-prod.bgchprod.info:443/omnia/auth/sessions",
        headers: {
          'Content-Type': 'application/vnd.alertme.zoo-6.1+json',
          'Accept': 'application/vnd.alertme.zoo-6.1+json',
          'X-Omnia-Client': 'Hive Web Dashboard',
          'cache-control': "no-cache",
        },
        method: "POST",
        body: JSON.stringify(body),
      }, function (error, response, body) {
        if (!error && response.statusCode == 200) {
          var responseJson = JSON.parse(body);
          // nested request after successfully obtaining the session id
          request({
            url: "https://api-prod.bgchprod.info:443/omnia/nodes",
            headers: {
              'Content-Type': 'application/vnd.alertme.zoo-6.1+json',
              'Accept': 'application/vnd.alertme.zoo-6.1+json',
              'X-Omnia-Client': 'Hive Web Dashboard',
              'cache-control': "no-cache",
              'X-Omnia-Access-Token': responseJson.sessions[0].sessionId,
            },
            method: "GET",
          }, function (error, response, body) {
            if (!error && response.statusCode == 200) {
              // the required data
              var data = JSON.parse(body);
            }
          });
        }
      });
    }
    

  • Module Developer

    @strawberry-3.141 Fantastic - just needed to be nested. That now works like a charm - thank you so much :)



  • @mongo116 the callback of the request gets executed async, so you can’t put them just next to each other. Because then the second request would be fired before the first one got his respond, by nesting you can ensure that the first request got a respond.


Log in to reply
 

Looks like your connection to MagicMirror Forum was lost, please wait while we try to reconnect.