Assign to a variable the content of a web page

Hi, I’m trying to write on aws shadow the content of a web page calling it via url using the api service. I succeed in writing on cloudwatch logs but not on shadow. In particular, I should write assign to UD2 the page content. Is there anyone that can help me? Thanks, Eleonora



            
console.log('Loading function');
var aws = require('aws-sdk');

var endpoint  = '';
var thingName = '';

exports.handler = function(event, context) {
  
  var https = require("https");

  var querystring = ""; 

    https.get(encodeURI(querystring), function(res) {
        console.log("Got response: " + res.statusCode);
        res.setEncoding('utf8');
        res.on('data', function(d) {
           //testtest = d;
            console.log('Body: ' + d);
            return d;
        });
        context.succeed(res.statusCode);
   
        //console.log("res:" + res);
        return res;
      }).on('error', function(e) {
        console.log("Got error: " + e.message);
        context.fail(e.message);
      });
      
       //console.log("STATO: ",stato);  
    
    var iotdata = new aws.IotData( { endpoint: endpoint } );
    var params = { thingName: thingName };
    iotdata.getThingShadow(params, function (err, data) {
        
        if (!err) {
            var payload = JSON.parse(data.payload);
            var currentUD1 = payload.state.desired.UD1;
            var currentUD2 = payload.state.desired.UD2;
            console.log("Current state : " + currentUD2);
          //  console.log("body: " +body);
           var desiredUD2;
            if(currentUD1 == 1) {
                  
           desiredUD2 = https.get.res.on.d;
            console.log(desiredUD2);
            }
              else {
          
            desiredUD2=https.get.res.on.d;  
            console.log(desiredUD2);
                 }  
                
            var desiredState = {
                state: {
                    desired: {                
          UD2:https.get.ren.on.d
                    },
                    reported: {
       UD2:https.get.res.on.d
                    }
                }
            };

            var params = {
                thingName: thingName,
                payload: JSON.stringify(desiredState)
            };
            iotdata.updateThingShadow(params, function (err, data) {
                if (!err) {
                    context.succeed();
                } else {
                    context.fail(err);      
                }
            });
        } else {
            context.fail(err);      
        }
    });
};          
    

forewarning: I am not knowledgable in AWS’ API.
That said, I’m not sure I can follow your logic just from a… logic perspective.

           if(currentUD1 == 1) {
                  
           desiredUD2 = https.get.res.on.d;
            console.log(desiredUD2);
            }
              else {
          
            desiredUD2=https.get.res.on.d;  
            console.log(desiredUD2);
                 }  

… This is the same thing twice. Why put an if here if both clauses do the same thing?

           var desiredState = {
                state: {
                    desired: {                
          UD2:https.get.ren.on.d
                    },
                    reported: {
       UD2:https.get.res.on.d
                    }
                }
            };

Why did we define desiredUD2 if we’re not going to use it here?

if (!err) { context.succeed(); } else { context.fail(err); }

Which function fires?

It fires this function (I found the sample code online) :iotdata.updateThingShadow(params, function (err, data). But the topic is how to assign the body of the webpage to UD2. Yes, You’re right, desiredUD2 is not used but is in the aws shadow so I would write on it as well.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.