onReceive returns -400 responseCode with Null data after a JSON web request

Hi, I am trying to connect to a web server to receive some data from an external web server. When looking at the HTTP traffic log on the simulator I can see I get an HTTP 200 OK response back and some  data in the body. However, when I parse the data in onReceive, its Null and the reponseCode is -400. Here is the code I use:

var headers = { 
	"Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON,	
	"Accept" => "application/json"};
		
	
var params = {
	"password" => _pwd,
	"accountName" => _username,};
			
var options = { 
	:method => Communications.HTTP_REQUEST_METHOD_POST,
	:headers => headers};
	
Communications.makeWebRequest(loginURL, params, options, method(:onReceive));


function onReceive(responseCode, data) {
    	System.println("ResponseCode: " + responseCode);
    	System.println("Data: " + data);
}

The reponseCode I get in onReceive is -400 and data is Null. 

When I look at the HTTP traffic log in the Simulator I see this:

To me it appears the server sends back an OK response to me but for some reason the onReceive function cannot parse the data. Am I doing anything wrong?

  • The error is:

    INVALID_HTTP_BODY_IN_NETWORK_RESPONSE = -400

    for ID 221, that doesn't seem to be JSON data

  • Hi Jim, I am not entirely sure how JSON data is supposed to look but I do know other clients (non-Garmin) will parse the response data just fine. The samples I have seen use content type and response type set to "application/json". I have set them to "Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON" in my app but does not work. 

  • *** SOLVED ***

    I changed the content type to "Communications.HTTP_RESPONSE_CONTENT_TYPE_TEXT_PLAIN" and now it works! Thanks Jim for pointing me to the right direction. 

  • With PLAIN_TEXT, you want to keep any eye out which devices you target, as not all support it.

  • Look at item 209.  There you are sending data in JSON format.

  • okey, got it. You mentioned not all devices support PLAIN_TEXT, is it possible to find out which ones? Thanks!

  • It's in the API Doc:

    HTTP_RESPONSE_CONTENT_TYPE_TEXT_PLAIN = 5

    Content type specifier for response is expected to be plain text type. Content type string must be “text/plain”

    Since:

    • 3.0.0

  • Hi Jim, I am not entirely sure how JSON data is supposed to look but I do know other clients (non-Garmin) will parse the response data just fine

    The value being returned is a JSON string, which is one of the valid JSON value types, but the ConnectIQ communications module currently expects a JSON object or array.

  • Well, it seems I'm out of luck again. Whereas the Fenix 5 in the simulator worked well with HTTP_RESPONSE_CONTENT_TYPE_TEXT_PLAIN, when running the app on the target (Fenix 5S) I get the same problem as before: ResponseCode -400 and null data. is there any known issues with using HTTP_RESPONSE_CONTENT_TYPE_TEXT_PLAIN  on a Fenix 5?

    I've set the min SDK to 3.0.0 in my manifest file 

  • Here is my HTTP request, it works just fine on the simulator but not on target.  

    var headers = { 
    					"User-Agent" => "some_hidden_stuff...",	
    					"Content-Type" => Comms.REQUEST_CONTENT_TYPE_JSON };
    					
    	
    				var params = {
    					"password" => m_password,
    					"accountName" => m_username,
    					"applicationId" => m_applicationID };
    		
    				var options = { 
    					:method => Comms.HTTP_REQUEST_METHOD_POST,
    					
    					:responseType => Comms.HTTP_RESPONSE_CONTENT_TYPE_TEXT_PLAIN,
    					:headers => headers };

    from the HTTP traffic log:

    POST /ShareWebServices/Services/General/LoginPublisherAccountByName HTTP/1.1\r\nHost: <hostname_here>\r\nAccept: */*\r\nContent-Type: application/json\r\nUser-Agent: Dexcom%20Share/3.0.2.11 CFNetwork/711.2.23 Darwin/14.0.0\r\nContent-Length: 109\r\n\r\n

    obfuscating the hostname...