makeWebRequest: -201 response when sending a POST request containing a JSON array

Former Member
Former Member
I am trying to send a POST request containing a JSON array in the body. The array would contain one or more JSON objects Using the syntax I have tried, the requests aren't successful and I receive a -201 response (INVALID_HTTP_BODY_IN_REQUEST).

I have tried using the syntax in the test, test2 and test3 variables below. I would have provided the full code snippet but it seems to break the forum editor (Error Loading Preview/.

function sendAnswers() {
var url = "https://example.url/" + id + "/answers";

var test = [ {
"a" => "test",
"b" => "test",
"c" => "test"
}, {
"a" => "test",
"b" => "test",
"c" => "test"
} ];

// [{a=>test, b=>test, c=>test}, {a=>test, b=>test, c=>test}]

var test2 = "[{ \"a\": \"test\", \"b\": \"test\", \"c\": \"test\" }]";

// [{ "a":"test", "b":"test", "c": "test" }]

var test3 = "[" +
{ "a" => "test",
"b" => "test",
"c" => "test" }
+ "]";

// [{a=>test, b=>test, c=>test}]

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

Comm.makeWebRequest(url, test, options, method(:onReceive));
}


var test = [ {
"a" => "test",
"b" => "test",
"c" => "test"
}, {
"a" => "test",
"b" => "test",
"c" => "test"
} ];

// [{a=>test, b=>test, c=>test}, {a=>test, b=>test, c=>test}]

var test2 = "[{ \"a\": \"test\", \"b\": \"test\", \"c\": \"test\" }]";

// [{ "a":"test", "b":"test", "c": "test" }]

var test3 = "[" +
{ "a" => "test",
"b" => "test",
"c" => "test" }
+ "]";

// [{a=>test, b=>test, c=>test}]




Is it possible to POST a JSON request body containing an array with makeWebRequest?
  • Willii,

    I apologize for not reporting back sooner, but I did file an issue for this. I actually made a request to update {{makeWebRequest}} to allow the caller to provide parameters in addition to the message body. This should allow you to do what you want (put arbitrary json into the body) and what others have asked for (specify request parameters in addition to a body).

    Travis
  • Former Member
    Former Member over 7 years ago
    No problem and thanks for filing the issue Travis. I'm guessing that I can't track it's progress?
  • Correct. There is no public access to our bug tracker. That said, I can tell you that the issue has been backlogged for the time being. While this issue itself isn't complicated, there are many other bits of code that are affected and we want to try to get them fixed in lock-step.

    Travis
  • Former Member
    Former Member over 6 years ago
    Has this issue been resolved? I'm trying to make a POST request to a graphQL API, and while it works as expected in the simulator (the GraphQL service I'm querying returns a 200 response with an appropriate response body) but when I test on-device (FR935), I get back a -201 status and an empty response body. Firmware 11.00 on the 935, and version 4.11.0.11 of Garmin Connect for iPhone/iOS
  • Former Member
    Former Member over 6 years ago
    For clarity, my code looks like this:

    var params = {
    "query" => "query my_query($lat: Float!, $lon: Float!, ...",
    "variables" => {
    "lat" => lat,
    "lon" => lon,
    "maxdistance" => maxdistance,
    "maxstops" => maxstops,
    "maxdep" => maxdepartures,
    }
    };
    var options = {
    :method => Communications.HTTP_REQUEST_METHOD_POST,
    :headers => {
    "Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON,
    },
    :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON
    };
    Communications.makeWebRequest(API_URL, params, options, callback);


    For clarity, I've manually truncated the query as it's a bit over 500 bytes long (in case that would make a difference)
  • Is this issue been resolved? 

    I am trying to implement the same graphql API in the Watch app from hours can you please look in to this

    function makeRequest() as Void {
    System.println("makeRequest");
    var url = API_URL;

    var params = {
    "query" => "query .......",
    "variables" => {
    "height" => "157"
    }
    };
    var headers = {
    "Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON,
    "Authentication" => "Bearer API_KEY"
    };


    var options = {
    :method => Communications.HTTP_REQUEST_METHOD_POST,
    :headers => headers,
    :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON
    };

    Communications.makeWebRequest(url, params, options, method(:onReceive));
    }

    i am getting Response code: 400
  • What is the URL you are using?  400 may mean it's not found on the server.

    Your app should use https and not http (unless the host is 127.0.0.1 or localhost).  Does the server handle https?

  • API_URL(https://Somthing) is tested on Postman giving a response, could you please suggest a way to integrate graphQl API or any sample snippet for reference?

    one question API requires authentication so what is the suggested Content-Type for it?

  • What are you expecting to get back?  A Graph? Depending on what it is, you may want to look into makeImageRequest.

    What do you see in the sim with "view http traffic"?

    In my apps I draw the graphs using monkey c.

  • My API returns a dictionary, but when making a GraphQL call through 'makeWebRequest,' I'm not receiving a response code of 200. below is there code snippet I am using

    function makeRequest() as Void {
    System.println("makeRequest");
    var url = API_URL;

     

    var params = {
    "query" => "query .......",
    "variables" => {
    "height" => "157"
    }
    };
    var headers = {
    "Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON,
    "Authentication" => "Bearer API_KEY"
    };



    var options = {
    :method => Communications.HTTP_REQUEST_METHOD_POST,
    :headers => headers,
    :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON
    };

     

    Communications.makeWebRequest(urlparamsoptionsmethod(:onReceive));
    }
    function onReceive(responseCode as Number, data as Dictionary<String, Object?> or String or Null) as Void {
    System.println("Response code: " + responseCode.toString());
    }