String Differences for RESTlet Post Method

In SuiteScript 2.0, a JSON.stringify() call is added internally to whatever is passed in the post() method of a RESTlet, which affects the value passed. In SuiteScript 2.1, a JSON.stringify() call is not added to the post() method for a RESTlet.

Scenario

SuiteScript 2.0 Behavior

SuiteScript 2.1 Behavior

Return JSON.stringify in a post function:

                    // the following is included in a 2.x RESTlet
...
function post() {
    return JSON.stringify("flower");
} 

                  

Returns "\"flower\"" and a string length of 12.

If you specify "flower" in the return statement, "flower" is returned with a string length of 8.

Returns "flower" and a string length of 8.

If you specify "flower" in the return statement, flower is returned with a string length of 6.

                    let xmlRequest = new XMLHttpRequest();
var url = "/app/site/hosting/restlet.nl?script=RESTLET_SCRIPT_ID&deploy=RESTLET_DEPLOYMENT_ID";

xmlRequest.onreadystatechange = () => {
    if (xmlRequest.readyState === 4) {
        if (xmlRequest.status === 200) {
            log.debug("xmlRequest.responseText=" + xmlRequest.responseText);
            log.debug("xmlRequest.responseText.length=" + xmlRequest.responseText.length);
        } else {
            log.debug("xmlRequest.status=" + xmlRequest.responseText);
        }
    }
};

xmlRequest.open("POST", url, true /* async */);
xmlRequest.setRequestHeader("Content-Type", "application/json");
xmlRequest.send(JSON.stringify({})); // log statements appear after request is sent 

                  

The log statements are:

xmlRequest.responseText="\"ok\""

xmlRequest.responseText.length=8

The log statements are:

xmlRequest.responseText="ok"

xmlRequest.responseText.length=4

Related Topics

General Notices