Invalid value when making a web request

Hello,

When I try to make a webRequest in a widget

 the code:

using Toybox.System;
using Toybox.Communications;
using Toybox.Lang;

class JsonTransactionTest {
    function onReceiveTest(responseCode as Lang.Number, data as Null or Lang.Dictionary or Lang.String) as Void{
        if (responseCode == 204) {
            System.println("Request Successful");                 
        } else {
            System.println("Response: " + responseCode);            
        }

    }

    function makeRequestLocalHost() as Void {
        var url = "http://192.168.1.117:8080/position";
        var latitudePosition= 0;
        var longitudePosition= 0;
        var params = {
            "latitude" => latitudePosition,
            "longitude" => longitudePosition,                          
        };

        var options = {                                            
            :method => Communications.HTTP_REQUEST_METHOD_GET,     
            :headers => {
            "Content-Type" => Communications.REQUEST_CONTENT_TYPE_URL_ENCODED
            },
            :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON
        };
        Communications.makeWebRequest(url, params, options, method(:onReceiveTest));
        System.println("makeRequest() done"); 
    }
}

I get this on the debug console:

Error: Invalid Value
Details: Failed invoking <symbol>
Stack: 
  - makeRequestLocalHost() at C:\Users\user\Documents\garmin\widget12\source\widgetHttp.mc:32 0x100000a6 
  - onTap() at C:\Users\user\Documents\garmin\widget12\source\widgetInputTest.mc:18 0x100002ba 
  - handleEvent() at 704b03c0.mb:1265 0x30002a32 
  - handleEvent() at 704b03c0.mb:1474 0x30002cf9 

I call the method by doing a onTap.

I still have the error if I make a https request

  • You can onlyy use http for "local host" (127.0.0.1), which means the service is on your phone.  Otherwise you need https.

    See https://forums.garmin.com/developer/connect-iq/i/bug-reports/unable-to-use-http-on-192-168-x-x

  • If it isn't possible, is it possible to trigger a function in react native that calls this http address by doing a onTap or something else on the Garmin widget? (I'm pretty sure the answer will be no but still asking anyway)

  • Based on android vs iOS, it's the phone itself that requires https.

  • I'm coming back to this post because I had put the garmin project aside to work on the http bridge. Now I have a http bridge on my react native app but I still can't make calls on Garmin. I used this code with https that I found in a sample in the garmin sdk to test if it works but it didn't work in my widget (but the sample app itself works)

    import Toybox.System;
    import Toybox.Communications;
    import Toybox.Lang;
    
    class JsonTransactionTest {
        function onReceiveTest(responseCode as Number, data as Null or Dictionary or String) as Void{
            if (responseCode == 204) {
                System.println("Request Successful");                 
            } else {
                System.println("Response: " + responseCode);            
            }
    
        }
    
        function makeRequestLocalHost() as Void 
        {
            var options = {
                :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON,
                :headers => {
                    "Content-Type" => Communications.REQUEST_CONTENT_TYPE_URL_ENCODED
                }
            };
            Communications.makeWebRequest(
            "https://jsonplaceholder.typicode.com/todos/115",
            {},
            options,
            method(:onReceiveTest));
        }
    }

    After this I found this post for widget https://forums.garmin.com/developer/connect-iq/f/discussion/167914/could-not-find-symbol-method#

    So in my code it would be like this

            var callable =new Method($,:onReceiveTest);
             Communications.makeWebRequest(
                "https://jsonplaceholder.typicode.com/todos/115",
                {},
                options,
               callable);

    but with this I have this error on compil

    Invalid '$.Toybox.Lang.Method' passed as parameter 4 of type 
    'PolyType<(callback(responseCode as $.Toybox.Lang.Number,
    data as Null or $.Toybox.Lang.Dictionary or $.Toybox.Lang.String) as Void)
    or (callback(responseCode as $.Toybox.Lang.Number,
    data as Null or $.Toybox.Lang.Dictionary or $.Toybox.Lang.String, 
    context as $.Toybox.Lang.Object) as Void)>'.

    So I tried to add "context as Object" in onReceiveTest in the params but I have the same error

  • Ok so with the sdk 4.1.3, I succeed to go past the Communications.makeWebRequest with the 

    var callable = new Method($,:onReceiveTest)

    but it still crash afterwards and it doesn't do the webrequest.

    And I have this warning for the var callable

    Unable to detect scope 'Method' for the symbol 'initialize'.

  • 4.1.3 doesn't have type checking turned on my default.

    Try changing

    var callable = new Method($,:onReceiveTest)

    to

    var callable = method(:onReceiveTest)

  • okok but when I do what you say I have

    Error: Invalid Value
    Details: Failed invoking <symbol>
    

  • Since there isn't a line number, add some println calls including on inReceive..  

  •     public function onReceiveTest(responseCode as Number, data as Null or Dictionary or String,context as Object) as Void{
            System.println("onReceiveTest() called \n");
            System.print(responseCode);
            if (responseCode == 200) {  
             
                System.println("Request Successful \n");                 
            } else {
               
                System.println("Response: " + responseCode);            
            }
    
        }
       
    
         function makeRequest() as Void {
               var options = {
                :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON,
                :headers => {
                    "Content-Type" => Communications.REQUEST_CONTENT_TYPE_URL_ENCODED
                }
            };
            System.print("before callable \n");
            var callable = method(:onReceiveTest);
            System.print("before makeWebRequest() \n");
             Communications.makeWebRequest(
                "https://jsonplaceholder.typicode.com/todos/115",
                {},
                options,
               callable);
            System.println("makeRequest() done \n"); 
        }

    and the result

    initialize 
    onTap
    before callable