Comm.makeWebRequest Bug - when sending boolean parameters

Former Member
Former Member
I try to send boolean parameters using:

... makeWebRequest(url, {"on"=>true}, options, responseCallback);

I do send this request as PUT and use REQUEST_CONTENT_TYPE_JSON.

But the parameter is sent as

{"on":1}

So, true is converted to 1.

As far as I know, this is no valid JSON anymore.
How can I make sure to send proper boolean values?






  • Coleman mentioned that the problem had been identified and forwarded to the Garmin Connect team. This implies that the problem won't be fixed with a ConnectIQ update. You'll have to wait for a fix to Garmin Connect Mobile...

    Travis
  • Looks like this was fixed in Garmin Connect Mobile, but it's still broken in the simulator.
  • Former Member
    Former Member
    Here I am 6 months later starting to write a Garmin Connect IQ app, and I saw that this is still an issue with Connect SDK 2.3.4. Now, though, { "on" => true } gets transformed to { "on" => True } (uppercase "T") as part of the payload, resulting in invalid JSON.

    Looks like this was fixed in Garmin Connect Mobile, but it's still broken in the simulator.


    Can anyone confirm that, please?

    Also, is there any word when this will be fixed in the simulator? It makes testing requests that toggle state difficult (and I don't want to push to my real device every time).
  • I cannot reproduce this with a local test case on the simulator using the 2.3.4 SDK.

    The following is my simple python HTTP server..

    import BaseHTTPServer
    import json
    
    class MockRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    
        def init(self):
            pass
    
        def do_GET(self):
            message_body = {
            "array" : [ "A" ],
            "dict" : { 1 : "2" },
            "float" : 1.23,
            "double": 3.1415926535897932,
            "number": 123,
            "long" : 2147483648,
            "string": "hello",
            "boolean": True
            }
    
            content_type = "application/json"
            message_body = json.dumps(message_body)
    
            self.send_response(200)
            self.send_header("Content-Type", content_type)
            self.send_header("Content-Length", len(message_body))
            self.end_headers()
            self.wfile.write(message_body)
    
            print 200
            print "Content-Type: {}".format(content_type)
            print "Content-Length: {}".format(len(message_body))
            print ""
            print message_body
    
    def _main():
        server_address = ('', 8080)
        httpd = BaseHTTPServer.HTTPServer(server_address, MockRequestHandler)
        httpd.serve_forever()
    
    if __name__=='__main__':
        _main()



    Here is my MonkeyC application code.

    using Toybox.Application as App;
    using Toybox.Lang as Lang;
    using Toybox.System as Sys;
    using Toybox.WatchUi as Ui;
    using Toybox.Graphics as Gfx;
    using Toybox.Communications as Comm;
    
    class TestBehaviorDelegate extends Ui.BehaviorDelegate
    {
        hidden var _view;
        hidden var _pending;
    
        function initialize(view) {
            BehaviorDelegate.initialize();
            _view = view;
            _pending = false;
        }
    
        function onSelect() {
            if (!_pending) {
                _pending = true;
    
                var url = "http://localhost:8080";
    
                var params = {
                };
    
                var options = {
                //:headers => {
                // "Content-Type"=> Comm.REQUEST_CONTENT_TYPE_JSON
                //},
    
                //:method => Communications.HTTP_REQUEST_METHOD_GET,
    
                //:responseType => Comm.HTTP_RESPONSE_CONTENT_TYPE_FIT
                //:responseType => Comm.HTTP_RESPONSE_CONTENT_TYPE_JSON
                };
    
                Comm.makeWebRequest(url, params, options, method(:onWebResponse));
                _view.onWebRequest(url);
            }
    
            return true;
        }
    
        function onWebResponse(code, data) {
            _view.onWebResponse(code, data);
            _pending = false;
        }
    }
    
    class TestView extends Ui.View
    {
        hidden var _code;
        hidden var _data;
        hidden var _pending;
    
        function initialize() {
            View.initialize();
            _pending = false;
        }
    
        function onUpdate(dc) {
            dc.setColor(Gfx.COLOR_WHITE, Gfx.COLOR_BLACK);
            dc.clear();
    
            var cy = dc.getHeight() / 2;
            var cx = dc.getWidth() / 2;
    
            var color = Gfx.COLOR_GREEN;
            if (_pending) {
                color = Gfx.COLOR_RED;
            }
    
            dc.setColor(color, color);
            dc.fillCircle(cx, 10, 7);
            dc.setColor(Gfx.COLOR_WHITE, Gfx.COLOR_BLACK);
    
            if (_code != null) {
                var fy = dc.getFontHeight(Gfx.FONT_LARGE);
    
                dc.drawText(cx, cy - fy, Gfx.FONT_LARGE, _code.toString(), Gfx.TEXT_JUSTIFY_CENTER);
    
                if (_data != null) {
                    dc.drawText(cx, cy, Gfx.FONT_XTINY, _data.toString(), Gfx.TEXT_JUSTIFY_CENTER);
                }
            }
        }
    
        function onWebRequest(url) {
            _pending = true;
            Ui.requestUpdate();
        }
    
        function onWebResponse(code, data) {
            Sys.println(code);
            Sys.println(data);
    
            if (data != null) {
                data = data.toString();
            }
    
            _code = code;
            _data = data;
    
            _pending = false;
            Ui.requestUpdate();
        }
    }
    
    class TestApp extends App.AppBase
    {
        function initialize() {
            AppBase.initialize();
        }
    
        function getInitialView() {
            var view = new TestView();
            return [ view, new TestBehaviorDelegate(view) ];
        }
    }
    


    When I run the test, the server outputs...

    C:\Test>python test.py
    127.0.0.1 - - [06/Nov/2017 06:45:14] "GET / HTTP/1.1" 200 -
    200
    Content-Type: application/json
    Content-Length: 151

    {"boolean": true, "dict": {"1": "2"}, "string": "hello", "double": 3.141592653589793, "array": ["A"], "float": 1.23, "number": 123, "long": 2147483648}


    And the client outputs...

    200
    {number=>123, dict=>{1=>2}, string=>hello, boolean=>true, array=>[A], long=>2147483648, float=>1.230000, double=>3.141593}


    Travis

  • I do see a problem here, but it isn't the one that you are speaking of. The issue I see is that the value that should be a double is being truncated to a Float. I'm not certain how it behaves on devices, but I will be looking into that one a bit further.
  • Travis, I think Blaskovicz is talking about the data sent to the server, not the data which is returned.

    I can re-produce this with SDK 2.3.5 and it can shown simply by modifying the WebRequest sample code. If you change "Monkeys" => "Awesome" to "Monkeys" => true, what you get returned by the server is "Monkeys: True" so it appears that we are changing "true" to "True".

    If I use my own web server and look at the log, I can confirm this transformation:

    xx.xx.xx.xx - - [06/Nov/2017:17:07:41 +0000] "GET /get?ConnectIQ=1337&Monkeys=True HTTP/1.1" 404 3017 "-" "Mozilla/5.0"

    I believe this is what Blaskovicz is reporting, even though we set the value to true, what gets passed by the makeWebRequest is True capitalised.



  • Blaskovicz, I can confirm that this is fixed on a real watch. On my fenix 5X running the latest 6.75 beta firmware and the latest Android GCM 4.0.7 I get true without it being capitalised. So, this does seem specific to the simulator environment.
  • Looks like we have addressed several of the issues in this thread, including the true/True issue here discussed at the end. I'm going to create a new report and have that investigated, but I'll close this thread since it's a little old. Please report existing or new issues of this nature to the Connect IQ Bug Reports forum. Thanks!