I tried the example given in the doc exactly as it was written:
// It is common for developers to wrap a makeWebRequest() call in a function
// as displayed below. The function defines the variables for each of the
// necessary arguments in a Communications.makeWebRequest() call, then passes
// these variables as the arguments. This allows for a clean layout of your web
// request and expandability.
using Toybox.System;
using Toybox.Communications;
// set up the response callback function
function onReceive(responseCode, data) {
if (responseCode == 200) {
System.println("Request Successful"); // print success
}
else {
System.println("Response: " + responseCode); // print response code
};
};
function makeRequest() {
var url = "https://www.garmin.com"; // set the url
var params = { // set the parameters
"definedParams" => "123456789abcdefg"
};
var options = { // set the options
:method => Communications.HTTP_REQUEST_METHOD_GET, // set HTTP method
:headers => { // set headers
"Content-Type" => Communications.REQUEST_CONTENT_TYPE_URL_ENCODED},
// set response type
:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_URL_ENCODED
};
var responseCallback = method(:onReceive); // set responseCallback to
// onReceive() method
// Make the Communications.makeWebRequest() call
Communications.makeWebRequest(url, params, options, method(:onReceive));
}
But I get:
BUILD: ERROR: extraneous input ';' expecting '}'
BUILD: ERROR: extraneous input ';' expecting {<EOF>, 'class', 'module', 'using', 'import', 'alias', 'typedef', 'public', 'const', 'native', 'var', 'enum', 'function', '('}
I tried to fix the semicolons and put all inside a class:
// It is common for developers to wrap a makeWebRequest() call in a function
// as displayed below. The function defines the variables for each of the
// necessary arguments in a Communications.makeWebRequest() call, then passes
// these variables as the arguments. This allows for a clean layout of your web
// request and expandability.
using Toybox.System;
using Toybox.Communications;
class Test {
// set up the response callback function
function onReceive(responseCode, data) {
if (responseCode == 200) {
System.println("Request Successful"); // print success
}
else {
System.println("Response: " + responseCode); // print response code
}
}
function makeRequest() {
var url = "https://www.garmin.com"; // set the url
var params = { // set the parameters
"definedParams" => "123456789abcdefg"
};
var options = { // set the options
:method => Communications.HTTP_REQUEST_METHOD_GET, // set HTTP method
:headers => { // set headers
"Content-Type" => Communications.REQUEST_CONTENT_TYPE_URL_ENCODED},
// set response type
:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_URL_ENCODED
};
var responseCallback = method(:onReceive); // set responseCallback to
// onReceive() method
// Make the Communications.makeWebRequest() call
Communications.makeWebRequest(url, params, options, method(:onReceive));
}
}
But now I am getting:;Invalid '$.Toybox.Lang.Method(responseCode as Any, data as Any) as Any' 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)>'.
No idea what to do now. Why is the example not compiling?