Facility Control

 View Only
  • 1.  Using asyncpost with a URL that requires authentication

    Posted 10-28-2014 15:16
    Hey everyone,

    I am working on a task that uses ogscript to call a certain URL as a part of its functionality. The device at that URL requires basic authentication, so in a normal web browser, I can simply type in: http://username:password@myaddress/ and it will authenticate (versus if I left out the username/password bit, it would then prompt for the authentication).

    When I structure my ogscript asyncpost command with a URL string like I wrote above (including the username/password), I get a debug return of 401 (not authenticated). Anyone else run into this problem and worked around it?

    Thanks!

    Joseph


  • 2.  RE: Using asyncpost with a URL that requires authentication

    Posted 10-28-2014 15:54
    Hi Joseph.

    Unfortunately, there is no support for authentication built into the ogscript.asyncPost command.

    You can still do an authenticated http get using a listener tag or, if you don't care about the response (other than whether or not it went through), using rosstalk.sendMessage.

    Here is a "simple" example of using rosstalk.sendMessage to do an authenticated post. Note this will only wait around for the first line of the response from the server (which would be the http return code "HTTP/1.1 200 OK").

    //Example doing an authenticated post to http://browserspy.dk/password-ok.php

    var message = 'GET /password-ok.php HTTP/1.1\r\n' + //You will replace "/password-ok.php" with your desired path

    'Host: browserspy.dk\r\n' + //replace with your destination host/IP

    'Connection: close\r\n' +

    'Authorization: Basic dGVzdDp0ZXN0\r\n' + //this is a base64 encoding of 'test:test' [Username]:[password] you can do the encoding at https://www.base64encode.org/

    '\r\n?'; //HTTP headers end with a blank line. The question mark means that DashBoard will wait for a response

    function callback(success, sentData, resultString, exception)

    {

    ogscript.debug(resultString);

    }

    rosstalk.sendMessage('browserspy.dk', 80, message, callback);


    Hope that helps.

    James

    #DashBoard


  • 3.  RE: Using asyncpost with a URL that requires authentication

    Posted 10-28-2014 19:49
    Awesome. Sounds like for what I'm doing, I need to be using rosstalk.sendMessage anyway. Thanks for the code snippet! I'll get to work on the adaptation for my project.

    Joseph

    #DashBoard


  • 4.  RE: Using asyncpost with a URL that requires authentication

    Posted 10-28-2014 20:11
    Glad I could help. Remember, if you need to get the full reply from the web server instead of just the return code, you'll need to switch from rosstalk.sendMessage to a listener tag (slightly more complex but still doable).

    #DashBoard


  • 5.  RE: Using asyncpost with a URL that requires authentication

    Posted 10-28-2014 20:14
    For this particular scenario, I really just want to fire off a command to a device, and as long as it goes through, I don't really care about the response. I'm writing a button to turn a projector on, so visually in the room, I'll know if it went through or not.

    Good to know about the listener tag though - because that gives me ideas on another project! Ah, the life of programmers, I guess...

    #DashBoard