MagicMirror Forum

    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • Donate
    • Discord

    Passing variable from one request to another

    Troubleshooting
    2
    8
    2051
    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
      mongo116 Module Developer last edited by

      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

      strawberry 3.141 1 Reply Last reply Reply Quote 0
      • strawberry 3.141
        strawberry 3.141 Project Sponsor Module Developer @mongo116 last edited by

        @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
          mongo116 Module Developer last edited by

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

          strawberry 3.141 1 Reply Last reply Reply Quote 0
          • strawberry 3.141
            strawberry 3.141 Project Sponsor Module Developer @mongo116 last edited by

            @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
              mongo116 Module Developer last edited by mongo116

              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

              strawberry 3.141 1 Reply Last reply Reply Quote 0
              • strawberry 3.141
                strawberry 3.141 Project Sponsor Module Developer @mongo116 last edited by

                @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 Reply Quote 1
                • M
                  mongo116 Module Developer @strawberry 3.141 last edited by

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

                  strawberry 3.141 1 Reply Last reply Reply Quote 0
                  • strawberry 3.141
                    strawberry 3.141 Project Sponsor Module Developer @mongo116 last edited by

                    @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
                    • First post
                      Last post
                    Enjoying MagicMirror? Please consider a donation!
                    MagicMirror created by Michael Teeuw.
                    Forum managed by Paul-Vincent Roll and Rodrigo Ramírez Norambuena.
                    This forum is using NodeBB as its core | Contributors
                    Contact | Privacy Policy