makeJsonRequest's incorrect behavior in the simulator

Former Member
Former Member
I've found a discrepancy in the behavior of makeJsonRequest when in the simulator and on the watch. The POST requests sent by simulator use incorrect encoding of the request body when [FONT=Courier New]Comm.REQUEST_CONTENT_TYPE_URL_ENCODED[/FONT] is used. For the following code both the simulator and the watch will make requests with their [FONT=Courier New]Content-Type[/FONT] header is set to [FONT=Courier New]application/x-www-form-urlencoded[/FONT]. The difference is that the simulator sends params as a json blob while watch actually uses url encoding format.

var options = {
:method => Comm.HTTP_REQUEST_METHOD_POST,
:headers => {
"Content-Type" => Comm.REQUEST_CONTENT_TYPE_URL_ENCODED
}
};
var params = {
"param1" => someValue,
"param2" => "otherValue"
};
var url = "an.address.com/path";

Utils.makeJsonRequest(url, params, options, method(:_aCallback));


I've had this behavior on OS X when using SDK versions 1.2.1 - 1.2.5. I have not tried the simulator on windows.

As a workaround I wrote a proxy server which translates the requests into url encoded format. I run it locally and use the following wrapper in the code to use it:

function isSimulated() {
return System.getDeviceSettings().firmwareVersion.toString().equals("[0, 0]");
}

function makeJsonRequest(url, params, options, callback) {
if (isSimulated()) {
System.println("in sim: proxying " + url);
Comm.makeJsonRequest("localhost:8080/proxy + url, params, options, callback);
} else {
System.println("direct request: " + url);
Comm.makeJsonRequest(url, params, options, callback);
}
}


Is my experience consistent with other developers?
  • Yes, I can confirm...

    function onSelect() {

    var options = {
    :method => Comm.HTTP_REQUEST_METHOD_POST,
    :headers => {
    "Content-Type" => Comm.REQUEST_CONTENT_TYPE_URL_ENCODED
    }
    };

    var params = {
    "param1" => 1,
    "param2" => "otherValue",
    "param3" => true
    };

    var url = "http://127.0.0.1:8080";

    Comm.makeJsonRequest(url, params, options, method(:callback));

    return true;
    }

    function callback(code, data) {
    Sys.println(code);
    Sys.println(data);
    }


    ------------------

    -- method --------
    POST

    -- url -----------
    http://127.0.0.1:8080/

    -- get params ----

    -- headers -------
    Content-Length: 47
    User-Agent: Mozilla/5.0
    Host: 127.0.0.1:8080
    Accept: */*
    Content-Type: application/x-www-form-urlencoded

    -- post params ---
    {"param1":1, "param2":"otherValue", "param3":1}:

    -- body ----------
    {"param1":1, "param2":"otherValue", "param3":1}


    Here is the source for my simple python web service...

    import json
    import webapp2

    #
    # merge the given data into a json response
    #
    def jsonify(**kwargs):
    response = webapp2.Response(content_type="application/json")
    json.dump(kwargs, response.out)
    return response


    class DumpRequest(webapp2.RequestHandler):
    def any(self):

    print '\n------------------'
    print '\n-- method --------'
    print self.request.method

    print '\n-- url -----------'
    print self.request.url

    print '\n-- get params ----'
    for key in self.request.GET:
    print '{key}: {value}'.format(key=key, value=self.request.GET[key])

    print '\n-- headers -------'
    for key in self.request.headers:
    print '{key}: {value}'.format(key=key, value=self.request.headers[key])

    print '\n-- post params ---'
    for key in self.request.POST:
    print '{key}: {value}'.format(key=key, value=self.request.POST[key])

    print '\n-- body ----------'
    print self.request.body

    return jsonify(status='success')

    def get(self):
    return self.any();

    def put(self):
    return self.any()

    def post(self):
    return self.any()



    config = {
    }

    debug = True

    routes = [
    webapp2.Route('/', DumpRequest)
    ]

    app = webapp2.WSGIApplication(routes, debug, config)

    def main():
    from paste import httpserver
    httpserver.serve(app, host='127.0.0.1', port='8080')

    if __name__ == '__main__':
    main()