• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
MagicMirror Forum
  • Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
A New Chapter for MagicMirror: The Community Takes the Lead
Read the statement by Michael Teeuw here.

Passing variable from one request to another

Scheduled Pinned Locked Moved Troubleshooting
8 Posts 2 Posters 2.8k Views 2 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    mongo116 Module Developer
    last edited by Jul 26, 2017, 12:44 PM

    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

    S 1 Reply Last reply Jul 26, 2017, 2:00 PM Reply Quote 0
    • S Offline
      strawberry 3.141 Project Sponsor Module Developer @mongo116
      last edited by Jul 26, 2017, 2:00 PM

      @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

      Please create a github issue if you need help, so I can keep track

      1 Reply Last reply Reply Quote 0
      • M Offline
        mongo116 Module Developer
        last edited by Jul 26, 2017, 2:12 PM

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

        S 1 Reply Last reply Jul 26, 2017, 5:16 PM Reply Quote 0
        • S Offline
          strawberry 3.141 Project Sponsor Module Developer @mongo116
          last edited by Jul 26, 2017, 5:16 PM

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

          Please create a github issue if you need help, so I can keep track

          1 Reply Last reply Reply Quote 0
          • M Offline
            mongo116 Module Developer
            last edited by mongo116 Jul 26, 2017, 7:52 PM Jul 26, 2017, 7:50 PM

            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

            S 1 Reply Last reply Jul 26, 2017, 8:17 PM Reply Quote 0
            • S Offline
              strawberry 3.141 Project Sponsor Module Developer @mongo116
              last edited by Jul 26, 2017, 8:17 PM

              @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);
                      }
                    });
                  }
                });
              }
              

              Please create a github issue if you need help, so I can keep track

              M 1 Reply Last reply Jul 26, 2017, 9:03 PM Reply Quote 1
              • M Offline
                mongo116 Module Developer @strawberry 3.141
                last edited by Jul 26, 2017, 9:03 PM

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

                S 1 Reply Last reply Jul 27, 2017, 8:12 AM Reply Quote 0
                • S Offline
                  strawberry 3.141 Project Sponsor Module Developer @mongo116
                  last edited by Jul 27, 2017, 8:12 AM

                  @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.

                  Please create a github issue if you need help, so I can keep track

                  1 Reply Last reply Reply Quote 0
                  • 1 / 1
                  1 / 1
                  • First post
                    1/8
                    Last post
                  Enjoying MagicMirror? Please consider a donation!
                  MagicMirror created by Michael Teeuw.
                  Forum managed by Sam, technical setup by Karsten.
                  This forum is using NodeBB as its core | Contributors
                  Contact | Privacy Policy