Looking for a way to issue a URL based command

I’m working on project and need to know how to use execute script to send an http based command.

The command will look like this:
http://user:pass@192.168.0.1:25105/3?0262222F150F12FF=I=3

Once I get this working I will work on ways to make it more secure depending on how skills are compiled.

I’m sure this is a very simple task but I just haven’t been able to figure it out.

Thank you!!!

Tony

Hi @codemonkey2k5

I recommend trying to use this node module, Request, it can make some of this a little easier as there can be a lot to keep track of and will generate the command for you.

Download Request module into your skill

  1. Open your command line tool
  2. cd into your skill directory
  3. run npm install request

Example use

For this example, I used Request Catcher subdomain for testing

() => {
  
  //Load the request module
  var request = require('request');

  //Lets configure and request
  request({
      url: 'http://example.requestcatcher.com/test', //URL to hit
      method: 'POST',
      auth: {
        'user': 'Test', 
        'pass': '123'
      },
      body: 'Hello Hello! String body!' //Set the body as a string
  }, function(error, response, body){
      if(error) {
          console.log(error);
      } else {
          console.log(response.statusCode, body);
      }
  });
  }

Check out Request’s APIs for more information depending on your needs. Also be sure to check out Node.js HTTP and HTTPS APIs for more detailed information as well.

Hope you find this helpful :slight_smile:

Hi Joe, thank you for taking the time to assist. I feel like I am very close. But I can not get the request to pass auth to my device.

Here are two ways I tried this:

() => {

//Load the request module
var request = require(‘request’);

//Lets configure and request
request({
url: ‘http://192.168.0.4:25105/3?02622086000F11FF=I=3-’, //URL to hit
method: ‘POST’,
auth: {
‘user’: ‘111111’,
‘pass’: ‘22222222’
},
body: ‘Hello Hello! String body!’ //Set the body as a string
}, function(error, response, body){
if(error) {
console.log(error);
} else {
console.log(response.statusCode, body);
}
});
}

and

() => {
var request = require(‘request’);
request(‘http://111111:22222222@192.168.0.4:25105/3?02622086000F11FF=I=3-’, function (error, response) {
if (!error && response.statusCode == 200) {
console.log(error, response);
} else {
console.log(response.statusCode, response);
}
});
}

My result is always: 401 "401 Unauthorized: Password required"
Yet if i open chrome and paste the same URL (btw my user and pass have been changed in the above examples) it works just fine.

Basically I am issuing a one time command. The only time there will any web content returned is if there is an error.

Regards,
Tony

@codemonkey2k5

Try setting up your example like this:

() => {
  
  //Load the request module
  var request = require('request');

  //Lets configure and request
  request({
      url: 'http://192.168.0.4:25105/', //URL to hit
      method: 'POST',
      auth: {
        'user': 'Test', 
        'pass': '123'
      },
      body: 'Hello Hello! String body!' //Set the body as a string
  }, function(error, response, body){
      if(error) {
          console.log(error);
      } else {
          console.log(response.statusCode, body);
      }
  });
  }

I am uncertain where this part of your http request came from: 3?0262222F150F12FF=I=3 but it looks like this is what your server is expecting to receive your authentication type and username:password combination.

Check out this post for more information about this: http://stackoverflow.com/questions/3905126/how-to-use-http-client-in-node-js-if-there-is-basic-authorization

My server needs to see this as the complete url:
http://USER:PASSWORD@192.168.0.4:25105/3?02622086000F11FF=I=3-

Those numbers at the end are part of the url being passed to my server. Each number is a function.

Tried your example and got the same result.
All the documentation states that this is correct but it will not pass the basic auth.

Frustrated…

Ok these are some of the ways I’ve tried to get this thing to work… Some bomb out Jibo all together, the rest give me some variation of a 401 error. Man I really thought this was going to be an easy one.

() => {
  
  //Load the request module
  var request = require('request');

  //Lets configure and request
  request({
      url: 'http://192.168.0.4:25105/3?02622086000F11FF=I=3-
', //URL to hit
      method: 'POST',
      auth: {
        'user': 'Lightnin', 
        'pass': 'PASSWORD'
      },
      body: 'Looks Like we made it!' //Set the body as a string
  }, function(error, response, body){
      if(error) {
          console.log(error);
      } else {
          console.log(response.statusCode, body);
      }
  });
  }

() => {


var request = require('request');

request('http://Lightnin:PASSWORD@192.168.0.4:25105/3?02622086000F11FF=I=3-', function (error, response) {
 
  if (!error && response.statusCode == 200) {
    
     console.log(error, response);
     } else {
          
     console.log(response.statusCode, response);  
  

    }

});

}

() => {


var request = require('request');
var username = 'Lightnin',
    password = 'PASSWORD',
    url = 'http://' + username + ':' + password + '@192.168.0.4:25105/3?02622086000F11FF=I=3-'; 
 
request({url: url}, function (error, response, body) {

 if (!error && response.statusCode == 200) {
    
     console.log(error, response);
     } else {
          
     console.log(response.statusCode, response);  
  

    }

});

}

() => {
  
  //Load the request module
  var request = require('request');

  //Lets configure and request
  request({
      url: 'http://192.168.0.4:25105/3?02622086000F11FF=I=3-', //URL to hit
      method: 'POST',
      auth: {
        'user': 'Lightnin', 
        'pass': 'PASSWORD'
      },
      body: 'Hello Hello! String body!' //Set the body as a string
  }, function(error, response, body){
      if(error) {
          console.log(error);
      } else {
          console.log(response.statusCode, body);
      }
  });
  }

() => {

var request = require('request'),
        'user': 'Lightnin', 
        'pass': 'PASSWORD',
    url = "http://192.168.0.4:25105/3?02622086000F11FF=I=3-",
    auth = "Basic " + new Buffer(username + ":" + password).toString("base64");

request({
        url : url,
        headers : {
            "Authorization" : auth
        }
    },
    function (error, response, body) {
     if(error) {
          console.log(error);
      } else {
          console.log(response.statusCode, body);
      }
  });
}

() => {
var http = require('http');

var request = http.get({'hostname': 'http://192.168.0.4:25105/3?02622086000F11FF=I=3-',
                            'auth': 'Lightnin:PASSWORD'
                           }, 
                           function (response) {
                             console.log('STATUS: ' + response.statusCode);
                             console.log('HEADERS: ' + JSON.stringify(response.headers));
                             response.setEncoding('utf8');
                             response.on('data', function (chunk) {
                               console.log('BODY: ' + chunk);
                             });
                           });
request.end();
}

() => {
var request = require('request');

var username = 'Lightnin';
var password = 'PASSWORD';
var auth = 'Basic ' + new Buffer(username + ':' + password).toString('base64');

// auth is: 'Basic VGVzdDoxMjM='

var header = {'Host': 'http://192.168.0.4:25105', 'Authorization': auth};
var request = request('http://192.168.0.4:25105/3?02622086000F11FF=I=3-', header);

request.end();

request.on('response', function (response) {
  console.log('STATUS: ' + response.statusCode);
  console.log('HEADERS: ' + JSON.stringify(response.headers));
  response.setEncoding('utf8');
  response.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});
}

EDIT: Posted after your above post where you use a similar example.

@codemonkey2k5

I understand your frustration dude and I think I may have been taking you into a different direction. Try this and let me know if this helps:

() => {
  
  //Load the request module
  var request = require('request');
  
  var user = 'username';
  var pass = 'password';
  var url = 'http://' + user + ':' + pass + '@192.168.0.4:25105/3?02622086000F11FF=I=3-';

  //Lets configure and request
  request({
      url: url, //URL to hit
      method: 'POST',
      body: 'Hello Hello! String body!' //Set the body as a string
  }, function(error, response, body){
      if(error) {
          console.log(error);
      } else {
          console.log(response.statusCode, body);
      }
  });
  }

If not, I would use the first example I posted to show you how to set up HTTP request within the SDK. You can see your request in action when you run your skill if you navigate to http://example.requestcatcher.com/. Then you can use that working example to figure out how to tweak the above examples to meet your servers requirements.

Generally though, you likely don’t want to pass the credentials in a visible string like this and you may want to do further research to find how to set up your server to accept authentication through a hash or token.

That didn’t work either. It says: 401 "401 Unauthorized: Password required"
I think I’ve used this code already. I realize it’s not typically good to pass the authentication as I am but this traffic is isolated to the local LAN. I would be happy to change things once I can get ANYTHING to work.

That requestcatcher doesn’t require credentials so it always works.

Again I can post the URL mentioned above directly in to chrome and it functions perfectly. So this is not a bad URL username or password.

@codemonkey2k5 The specific errors you are seeing are likely related to additional credentials beyond username and password needed to authenticate the request. Keep digging through the documentation you are reading and reach out to their support if you keep running into issues. Your request is leaving Jibo and reaching the front door. You just gotta make sure you are providing the right keys. You got this dude :slight_smile:

I assure you that there are no other credentials needed. I can simply past a single url that looks exactly like this:

http://usernamegoeshere:passwordgoeshere@192.168.0.4:25105/3?02622086000F11FF=I=3-

directly in to my chrome browser and hit enter and the command is run. Nothing else is needed.
I have used this method for several years in connection with the server I am connecting to. I know it VERY WELL.

This is either a limitation of the SDK or of my knowledge of javascript. But it has nothing to do with the URL, User or Password that I am trying to pass.

This should have worked with a simple curl command, but that wont work either. Everything I am reading says my code is correct. Can you please ask someone in the Dev team to look in to this for me because I am way beyond frustrated and not inclined to continue this project at this point.

This should be very simple. And I think that I am beating my head up against a problem with your SDK.

Tony

So I did a quick test myself and was able to use request to authenticate with a site and get a 200 (OK) response. I’ll post the code that I used below so you can test it out yourself and see if the issue still happens, or only with the URL you’re posting to.

Both of these methods work perfectly for me:

  var request = require('request');

  // Direct
  request('http://jibo:jibo1@jibo.thewebpro.com/', function (error, response){
    if(!error) {
       console.log("request SUCCEED:",response.statusCode);
    } else {
     console.log("request error:",error);
    }
  });

  // With POST
  var user = 'jibo';
  var pass = 'jibo1';
  var url = 'http://' + user + ':' + pass + '@jibo.thewebpro.com';
  request({
      url: url,
      method: 'POST',
      body: 'Hello Hello! String body!' //Set the body as a string
  }, function(error, response, body){
      if(!error) {
          console.log("request SUCCEED:",response.statusCode);
     } else {
          console.log("request error:",error);
      }
      done('');
  });

For anyone reading this who doesn’t have time to try out the code, but wants to see something fun, just go to http://jibo.thewebpro.com/ (user: jibo, pass: jibo1) and enjoy! Hint: it’s an early 3D model I built for Jibo while I was trying to pass the time waiting for him to arrive.

Michael

@michael Thank you for verifying this on your end and providing these methods/examples.

@codemonkey2k5 I used both of Michael’s examples and I’m also getting a good 200 response. I understand from our direct messages there is something specific you are trying to do where the above generic examples we provided are not working with your URL.

I understand this is frustrating and I roped in @justin.w, our developer evangelist, to take a look at what you are trying to do as well. If there is any direction or further suggestions he can make, we will be sure to post them!

Thank you again for hanging in there as we help look into this.

It looks like there may be a problem with my dev environment as I get errors with Michael’s script as well.
As weird as this may seem, this is good news.

If I get Michael’s script to work, then my URL will work.
Thanks for taking the time and dealing with all my newb questions. I’ll talk with Justin.

I got a chance to work with Justin on this and I am happy to announce that I stumped him as well. :slight_smile:
For now this option is off the table for reasons that neither of us can understand. But we worked out an alternative method of pulling off what I wanted to do. So in the end it’s all good.

Thank you to everyone who helped with this.

Tony