Newbie question regarding makewebrequest example

Former Member
Former Member

Hey!

I am sorry if this is a dumb question or anything. But i simply want to test out and use the method makewebrequest but cannot make it work. I constantly get responseCode -400 and data that is null. My code is following (Its based on the template given from eclipse):

function getInitialView() {
  		var url = "https://www.bing.com"; 
	
	    // this is the data that you want to send out
	    var params = {};
		
	  	var	options = {                                            
	  	 	:method => Communications.HTTP_REQUEST_METHOD_GET                                                   
	 	};
	
	   	// make the Communications.makeWebRequest() call to send data
	    Communications.makeWebRequest(url, params, options, method(:onResponse));
    	
    	//Template stuff
        //return [ new dataRetrieveView(), new dataRetrieveDelegate() ];
    }
    
    	function onResponse(responseCode, data)
	{
		System.println("in onReceive, responseCode=" + responseCode + ", data=" + data);
	}
    

I have tried using different content-type and responseType, but i keep getting -400...

So i tried not setting any of these as i read that "if the content type is not specified, it will default to "application/json" for GET" and that if responseType is not given the system will attempt parsing the response both as JSON, then as URL ENCODED.

How should i modify this example to not get responseCode -400 constantly? Can i not use bing as an example?

Please help me, i am confused and frustrated

  • Try the url in the sample.  Make sure that works.

  • Former Member
    0 Former Member over 4 years ago in reply to jim_m_58

    Using the URL given in the sample, makes it work just fine :O. Both with JSON and TEXT_PLAIN. Hmmm, why does it not work with using for example google, bing or garmin?? Just curious :) 

  • I think it's because google.com, bing.com and garmin.com return a content-type of "text/html" in the response, which doesn't match the expected content type. I'm guessing the API is fine treating an application/json response as text, but is a bit stricter with other response types.

    Although the following comment is over 2 years old, it is from a Garmin employee and I have no reason to believe any thing has changed since then.

    If the makeWebRequest() example in the docs literally doesn't work because of this issue, maybe you should file a bug report about that.

    https://forums.garmin.com/developer/connect-iq/f/discussion/166737/makewebrequest-not-working-on-device

    The request you're making informs the system that the response type will be text/plain, and the data will conform to that type. When GCM app running on iOS/Android receives a response, it compares the expected content type with that of the actual response, and if they don't match it will return a -400 error code to the ConnectIQ app. I have verified (with curl) that the incoming response has a text/html content type, so this is most likely the source of the -400 you're seeing.

    As to why the simulator and the actual device show different results, this is a bit complicated. The ConnectIQ simulator has its own system for handling web requests (technically it has two completely different systems). GCM on Android and iOS each have their own code for this too. So there are three different implementations of the code for handling web requests.. This makes it easy for behaviors to vary across the various platforms and can lead to inconsistencies you're seeing here.

    Currently, the only way to guarantee consistent behavior is to be sure that the response Content-Type header matches the expected header value. In some cases, the only way to do this is to stand up an intermediary web service (like a proxy) that will receive the web request from GCM, make the request to apple.com, and then translate the response as needed to feed it back to GCM.

    I have had a service like this running in Google AppEngine for several years.