Facility Control

 View Only
  • 1.  Handling ogscript.asyncHTTP response code 401

    Posted 06-25-2025 15:52

    I am using ogscript.asyncHTTP() with the response code flag set to true.  I wish to prompt the user if the password is incorrect.

    The callback is normally executed and I can access the response code and URL i.e. code 200.  However, the callback is not executed when the password is incorrect.

    Instead, I see a message in the debug window "Server returned HTTP response code: 401 for URL: https://192.168.0.82:8443/api/nodes/self/access"

    Why is the callback not being called?  Is there any way to write code to handle the 401 error?



    ------------------------------
    Richard Hills
    ------------------------------


  • 2.  RE: Handling ogscript.asyncHTTP response code 401

    Posted 06-26-2025 02:18

    I also implemented a try-catch, but this did not work either.

          try {
    ogscript.asyncHTTP(url, method, "application/json", data, httpCallback, true);
    }
    catch(err) {
    ogscript.debug('***' + err.message);
    }


    ------------------------------
    Richard Hills
    ------------------------------



  • 3.  RE: Handling ogscript.asyncHTTP response code 401

    Posted 06-26-2025 08:47

    Hi Richard,

    So because you are getting a 401 response that indicates your request lacks valid authentication credentials, you might need to provide more information to authenticate via the header. When you wish to provide headers using this method instead of providing "application/json" in the 3rd argument, if you provide a json object you can fully build out your header that way.

    Here is an example for pulling weather data in Ottawa, ON Canada that uses headers:

    var url = "https://api.open-meteo.com/v1/forecast?latitude=45.4215&longitude=-75.6998&hourly=temperature_2m";
    var method = "GET";
    var data = null;
    
    var headers = {};
    headers["User-Agent"] = "Mozilla/5.0";
    headers["Accept"] = "application/json";
    headers["Connection"] = "close";
    
    ogscript.asyncHTTP(url, method, headers, data, function(response) {
        if (response != null && response != undefined) {
            try {
                var json = JSON.parse(response);
                
                var lat = json.latitude;
                var lon = json.longitude;
                var times = json.hourly.time;
                var temps = json.hourly.temperature_2m;
                
                ogscript.debug("Weather Forecast for Coordinates: " + lat + ", " + lon);
                ogscript.debug("Showing first 5 hourly temperatures:");
                
                for (var i = 0; i < 5; i++) {
                    var timeStr = times[i];
                    var tempStr = temps[i];
                    ogscript.debug(timeStr + " → " + tempStr + "°C");
                }
                
                ogscript.debug("Forecast retrieved successfully.");
                
            } catch (e) {
                ogscript.debug("Failed to parse JSON: " + e);
            }
        } else {
            ogscript.debug("No response received.");
        }
    });



    ------------------------------
    Antony Giraldo
    DashBoard Custom Panel Developer
    Ross Video Creative Services | Rocket Surgery Triggering and Control
    ------------------------------



  • 4.  RE: Handling ogscript.asyncHTTP response code 401

    Posted 06-26-2025 09:22

    Antony,

    Thank you for the reply. I will have a closer look at the code snipit you sent.

    I think what has confused me is that when the request fails with 401 error this is not passed to the callback, but 200 success is, all I get is null.

    I will keep digging.  Thank you



    ------------------------------
    Richard Hills
    ------------------------------



  • 5.  RE: Handling ogscript.asyncHTTP response code 401

    Posted 06-26-2025 12:11

    Antony,

    Just a quick note to say thank you.  The code above was very helpful.



    ------------------------------
    Richard Hills
    ------------------------------



  • 6.  RE: Handling ogscript.asyncHTTP response code 401

    Posted 06-26-2025 13:11

    Unfortunately only response codes 200 or 404 will typically cause the response to be sent to the callback.  When the server sends the headers with no response body (typical of an error 401), the ogScript code encounters an internal exception (which is logged to the debug view).  We can pass along this information to the team so they can consider still triggering the callback with other response codes when no response body is present.



    ------------------------------
    James Peltzer
    Ross Video
    ------------------------------