makeWebRequest returns responseCode -403 although traffic log shows 200

I'm making a web request and the weird thing is that I get a different responseCode in my code than in the traffic log.

I make a simple request like following:

function makeWebRequest(url, params, callback) {
	var options = {
		:method => Communications.HTTP_REQUEST_METHOD_GET,
		:headers => {
			"Content-Type" => Communications.REQUEST_CONTENT_TYPE_URL_ENCODED
		},
		:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON
	};
	Communications.makeWebRequest(url, params, options, callback);
}

function loadWeatherPreview(callback) {
	BackgroundUtil.makeWebRequest(
		"https://api.openweathermap.org/data/2.5/forecast",
		{
			"lat" => Props.get(Constants.PROP_LAST_LOC_LAT),
			"lon" => Props.get(Constants.PROP_LAST_LOC_LONG),
			"appid" => Props.get(Props.OWM_KEY),
			"cnt" => 7,
			"units" => "metric" // Celcius
		},
		callback
	);
}

In my callback I get -403 as result code and an empty data value, but as you can see in the screenshot, the traffic log shows that I get result code 200.

How can this be?

  • It has to do with CIQ catching an error when it gets the response.  in this case:

    NETWORK_RESPONSE_OUT_OF_MEMORY = -403

    Ran out of memory processing network response.

    You seem to be asking for a 7 day forecast which might be too large for the background.I'm guessing that's how you're using this.

  • When running into this problem again with a request that did work previously, how can I find out what is happening?

    All I can see with the http traffic monitor is that the body has 0.45KB and I get this error again (with today's weather info, just 462 signs as json).

    * Does the apps memory also count and has impact on this error?

    * how and where can I check what the limit is?

    Btw, are those error constants defined somewhere? Can I see them somewhere in the api docs?

  • In the API doc, they are defined in the communications module documentation.

    Yes, the max side does depend on the size of your app and available memory, and memory in a background process is much less than a full app, being as small as 28k

    What you can do if you are using a backgound to see where you're at as far as memory is use

    System.getDeviceStats() and then freeMemory, usedMemory, totalMemory

    Remember, it's not just the number of bytes in a response, but it's json data, and gets converted into a dictionary  iI you have 462 json objects, that can be larger than you expect

    Also, one thing about this type of data (weather data) is the size could vary based on things like if strings are returned that could be short or long.

  • And I'm talking about 462 chars only, so this is really little...

    I checked what you suggested, this is what I get in my callback function:

    Memory: used: 24920, total: 28688, free: 3768

    As I get -403 error with this memory usage, I think all I can do is trying to reduce the code that is placed inside my (:background) annotated classes. Anything else I could do?

  • Makewebrequest does use a dramatic amount of memory, in part I believe due to the dictionary format of the returned data, can you change the request to limit the data returned and maybe make it into 2 smaller requests?

  • the request itself does work and can't be made smaller anymore - could solve it already by removing my debugger class that helps me debug informations in a common format all over my code...

    In my case, inline functions would really help to make some reusable functions that don't add that much to memory

  • That is pretty tight for a background with comm.

    I'd look at cutting that - maybe moving some things you do in the background to the main app, and even in the main App, your AppBases is loaded when the background runs, so maybe look at moving things from there to you view class.

  • I'm still a little bit confused. In my background's service initialise function I have following state now (after doing some optimisations and cleaning):

    Memory: used: 19824, total: 28688, free: 8840

    I try to download a json file that has 0.9kB and I get the -403 response in this case. I'm a little bit confused why this happens as there should be enough free memory as far as I understand (yes, the json will be an object and may take a little more memory than the raw string json data, but still this shouldn't be that much more than the 0.9kB).

    Is there some way to check this even further?

  • can you comment out some code as a test and bring that 19k down?  Remember that can be things you're doing in your AppBase.