How does this curl command translate to "makeWebRequest"

How does this curl command translate to "makeWebRequest"

curl -X POST \
some.webpage.com/.../call \
-H 'content-type: application/json' \
-H 'x-access-token: 123321-2323232-1233444' \
-d '{"x": "123", "y":"456", "z": { "z1": "789"}}'



I tried the following, but it didn't work:

Comm.makeWebRequest(
//URL
"some.webpage.com/.../call",
//parameter
{"x"=>"123", "y"=>"456", "z"=>{"z1"=>"789"}},
//options
{
:method=>Comm.HTTP_REQUEST_METHOD_POST,
:headers=>{ "x-access-token"=>accessToken, "Content-Type"=>Comm.REQUEST_CONTENT_TYPE_JSON },
:responseType=>Comm.HTTP_RESPONSE_CONTENT_TYPE_JSON
},
//callback
method(:onResponse)
);


Is the issue related to the nesting in "z": { "z1": "789"} ?
Does anyone have a working example for POST with nested JSON Request-Body

Thanks for any hints !!
  • I'd love to post a real response, but I can't post more than a few words.
  • I'd love to post a real response, but I can't post more than a few words.


    Even a few words may help as I could not figure out with the existing documentation how this needs to be done.
  • If I run this curl command...

    curl -X POST https://httpbin.org/post -H 'content-type: application/json' -H 'x-access-token: 123321-2323232-1233444' -d '{"x": "123", "y":"456", "z": { "z1": "789"}}'
  • I get this response (under the simulator, when using the 2.2.3 SDK)...

    {
    "args": {},
    "data": OMITTED: causes problems with forum
    "files": {},
    "form": {},
    "headers": {
    "Accept": "*/*",
    "Connection": "close",
    "Content-Length": "44",
    "Content-Type": "application/json",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.29.0",
    "X-Access-Token": "123321-2323232-1233444"
    },
    "json": {
    "x": "123",
    "y": "456",
    "z": {
    "z1": "789"
    }
    },
    "origin": "96.65.215.49",
    "url": "https://httpbin.org/post"
    }
  • If I run this code...

    var url = "httpbin.org/post";

    var params = {
    "x" => "123",
    "y" => "456",
    "z" => {
    "z1" => "789"
    }
    };

    var options = {
    :method => Comm.HTTP_REQUEST_METHOD_POST,
    :responseType => Comm.HTTP_RESPONSE_CONTENT_TYPE_JSON,
    :headers => {
    "x-access-token" => "12345",
    "Content-Type" => Comm.REQUEST_CONTENT_TYPE_JSON
    }
    };

    Comm.makeWebRequest(
    url, params, options, self.method(:onWebResponse));
  • If I print the response, I see this (using the simulator on ConnectIQ SDK 2.2.3)

    Device id 1 name "A garmin device"
    Shell Version 0.1.0
    https://httpbin.org/post
    200
    {
    files => {},
    args => {},
    origin => 96.65.215.49,
    data => { "x":"123", "y":"456", "z":{"z1":"789"} },
    headers => {
    Host => httpbin.org,
    User-Agent => Mozilla/5.0,
    Accept => */*,
    X-Access-Token => 12345,
    Content-Type => application/json,
    Content-Length => 40,
    Connection => close
    },
    url => https://httpbin.org/post,
    json => {
    x => 123,
    y => 456,
    z => {
    z1 => 789
    }
    },
    form => {}
    }


    Aside from the formatting differences on output, This seems correct to me. Note that strings aren't quoted in that response and when writing out a dict the key/value separator is => instead of :.
  • Are you having problems with the simulator or a physical device? What SDK and firmware versions are in play? What mobile device are you using?
  • Are you having problems with the simulator or a physical device? What SDK and firmware versions are in play? What mobile device are you using?


    Thanks Travis for your help. Really appreciate it, esp the hint to check with https://httpbin.org

    In my case it was a stupid error, the api requires the geocoordinates as string

    // We need to pass geocoordinates as string to api
    var lat_string = latitude.toString();
    var lng_string = longitude.toString();

    No issue with nested data and the garmin api at all (using the simulator on ConnectIQ SDK 2.2.3 + 2.2.5)

    Thanks again, my issue is solved !!