Acknowledged

Unable of using makeWebRequest function

Hello, I'm using Connect IQ 7.2.1 and I'm facing some issues with the makeWebRequest function when using Venu3S as device.

I'm using the official documentation (https://developer.garmin.com/connect-iq/api-docs/Toybox/Communications.html) with this code:

    using Toybox.System;
   using Toybox.Communications;

       function onReceive(responseCode, data) {
           if (responseCode == 200) {
               System.println("Request Successful");
           }
           else {
               System.println("Response: " + responseCode);
           };
       };

       function makeRequest() {
           var url = "https://www.garmin.com";     

           var params = {                                      
                  "definedParams" => "123456789abcdefg"
           };

           var options = {
               :method => Communications.HTTP_REQUEST_METHOD_GET,  
               :headers => {"Content-Type" => Communications.REQUEST_CONTENT_TYPE_URL_ENCODED},
               :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_URL_ENCODED
           };

           var responseCallback = method(:onReceive);                  
           Communications.makeWebRequest(url, params, options, responseCallback);
      }
      
However, I do receive this error which I do not know how to solve:  
  venu3s: Invalid '$.Toybox.Lang.Method(responseCode as Any, data as Any) as Any' passed as parameter 4 of type 'PolyType<($.Toybox.Lang.Method(responseCode as $.Toybox.Lang.Number, data as Null or $.Toybox.Lang.Dictionary or $.Toybox.Lang.String or $.Toybox.PersistedContent.Iterator) as Void) or ($.Toybox.Lang.Method(responseCode as $.Toybox.Lang.Number, data as Null or $.Toybox.Lang.Dictionary or $.Toybox.Lang.String or $.Toybox.PersistedContent.Iterator, context as $.Toybox.Lang.Object) as Void)>'.

I'd really appreciate some support on this topic.

Parents
  • The type checker is complaining that onReceive() is untyped, but it wants the argument and return types to be specified because onReceive is being passed as a method into makeWebRequest (via responseCallback).

    To fix this problem:

    1) Add the following line to top of your source file:

    import Toybox.PersistedContent;

    2) Change the onReceive() signature to:

    function onReceive(responseCode as Number, data as Null or Dictionary or String or PersistedContent.Iterator) as Void

    The problem is that type checking is enabled by default, but a lot of the old sample code hasn't been updated with types.

    The other option is to simply disable type checking, but personally I wouldn't recommend that in the long term.

     I do immediately see a problem in the code—there is a missing ';' at the end of the line for the "definedParams" dictionary, though I'm not certain that's the cause of the error you're seeing

    That's not true.

    var params = {                                      
      "definedParams" => "123456789abcdefg"
    };

    The dictionary is params and is terminated by a semicolon (on the 3rd line above). The line with "definedParams" is a key/value pair and should not be terminated with a semicolon

Comment
  • The type checker is complaining that onReceive() is untyped, but it wants the argument and return types to be specified because onReceive is being passed as a method into makeWebRequest (via responseCallback).

    To fix this problem:

    1) Add the following line to top of your source file:

    import Toybox.PersistedContent;

    2) Change the onReceive() signature to:

    function onReceive(responseCode as Number, data as Null or Dictionary or String or PersistedContent.Iterator) as Void

    The problem is that type checking is enabled by default, but a lot of the old sample code hasn't been updated with types.

    The other option is to simply disable type checking, but personally I wouldn't recommend that in the long term.

     I do immediately see a problem in the code—there is a missing ';' at the end of the line for the "definedParams" dictionary, though I'm not certain that's the cause of the error you're seeing

    That's not true.

    var params = {                                      
      "definedParams" => "123456789abcdefg"
    };

    The dictionary is params and is terminated by a semicolon (on the 3rd line above). The line with "definedParams" is a key/value pair and should not be terminated with a semicolon

Children