RESTlet Return Type Difference

SuiteScript 2.1 changes the way that RESTlet responses are formatted for certain content types. When the Content-Type in the RESTlet header does not match the type that the RESTlet is returning, the results returned may not be what you are expecting.

Scenario

SuiteScript 2.0 Behavior

SuiteScript 2.1 Behavior

Set the Content-Type in a RESTlet header to application/json. For example, the following sample RESTlet script and sample code for calling the RESTlet.

                    /**
 /**
 * @NApiVersion 2.x
 * @NScriptType restlet
 */
define([], function() {
        function get() {
                var x = {name:"jane", age:20};
                return JSON.stringify(x);
        }
        return {
                get: get
        }
}); 

                  
                    var host = window.location.host;
var url = 'https://' + host + '/app/site/hosting/restlet.nl?script=customscript296&deploy=customdeploy1';
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
       alert(typeof xhttp.response);
 alert(typeof JSON.parse(xhttp.response));
    }
};
xhttp.open("GET", url, true);
xhttp.setRequestHeader('Content-Type', 'application/json');
xhttp.send(); 

                  

The response is automatically converted to a string and that string is returned instead of an object.

In the example scenario, the alert output is a string, SS2.0 gives string; but in SS2.1 it comes as object.

The return type for JSON will be an object.

In the example scenario, the alert output is an object.

Related Topics

General Notices