makeJsonRequest response type plain text

Hello, I am begginer with ciq sdk and trying to make this app work with SDK 1.2.1:

https://github.com/danielsiwiec/waypoints-app

This is the code in question:

class Http {

  function _req(url, params, options, callback) {
    if (Comms has :makeWebRequest ) {
			Comms.makeWebRequest(url, params, options, callback);
		} else if (Comms has :makeJsonRequest ) {
			Comms.makeJsonRequest(url, params, options, callback);
		} else {
      throw new Toybox.Lang.Exception("No Communication API");
    }
  }

  function get(url, callback) {

    var options = {
			:method => Comms.HTTP_REQUEST_METHOD_GET,
			:responseType => Comms.HTTP_RESPONSE_CONTENT_TYPE_JSON
		};

    _req(url, null, options, callback);
  }

  function post(url, body, callback) {

    var options = {
			:method => Comms.HTTP_REQUEST_METHOD_POST,
      :responseType => Comms.HTTP_RESPONSE_CONTENT_TYPE_TEXT_PLAIN
		};

    _req(url, body, options, callback);
  }
}

class HashView extends Ui.View {

	var text;
	var http;
	var locationStore;
	var analytics;

	function initialize() {
		View.initialize();
		http = new Http();
		locationStore = new LocationStore();
		analytics = new Analytics();
	}

  function callback(responseCode, data){
		if (responseCode == 200) {
			var lat = data["geo"]["lat"];
			var long = data["geo"]["long"];
			var name = data["name"];
			var location = new Position.Location({:latitude => lat, :longitude => long, :format => :degrees});
			text = "Location\n" + name + "\nadded.";
			try {
				locationStore.save(location, {:name => name});
			} catch (ex) {
				System.println(ex.getErrorMessage());
				text = "No wypt API";
			}
		} else {
			text = "Comms error or \nwrong hash";
		}
  	WatchUi.requestUpdate();
  }

Problem is all response content types are from 1.3.0 onward.

If I remove response type from code app gets wrong response code and goes to this line.

"Comms error or \nwrong hash";

function post(url, body, callback) {

var options = {
:method => Comms.HTTP_REQUEST_METHOD_POST,
:responseType => Comms.HTTP_RESPONSE_CONTENT_TYPE_TEXT_PLAIN
};

_req(url, body, options, callback);
}

Any way around this, using only available makeJsonRequest?

Thanks,

Igor.

  • Ok, I'm not sure why you're trying to use the 1.2.1 SDK, as that one is old - from early 2016.  The current SDK is 3.0.11.

    The only device with CIQ 1.2.x is the Epix, but you can still use the 3.0.11 SDK to build for it.

    Also HTTP_RESPONSE_CONTENT_TYPE_TEXT_PLAIN is only available on devices running CIQ 3.0.0 or later, so roughly devices like the fenix 5 and newer.

  • Ok, I managed to get sendpoints app working on my Epix.

    It gets and saves location from hash 4 digit code correctly.

    I removed the ":responseType" line and added

    "Content-Type" => Toybox.Communications.REQUEST_CONTENT_TYPE_JSON(don't know if this makes it though, but response from the server is json formatted).

  • Which is different than plain text as only json was available in 1.2.x.

    BTW, with the Epix, the sim isn't always in line with the the actual device