How can I pass values to Rest API with new line and special characters

hetalvadanlal
Tera Contributor

Hi,

We have a unique requirement to communicate create an incident from one ServiceNow instance to other ServiceNow instance

The issue we are facing is that there are some fields like work notes and description which have new line character.

Whenever there there is a new line being set using "setStringParameter("variable_name", 'variable value with new line')", the incident creation does not happen

Below is the script

var r = new RESTMessage('TestIncidents', 'post');

r.setStringParameter("short_description", current.short_description);

r.setStringParameter("u_affected_user", current.u_affected_user);

r.setStringParameter("location", current.location);

r.setStringParameter("impact", current.impact);

r.setStringParameter("urgency", current.urgency);

r.setStringParameter("description", current.description);

r.setStringParameter("work_notes", current.work_notes);

r.setStringParameter("u_category", current.u_category);

r.setStringParameter("u_classification_level_3", current.u_classification_level_3);

r.setStringParameter("service_offering", current.service_offering);

r.setStringParameter("cmdb_ci",current.cmdb_ci);

r.setStringParameter("u_requestor", current.u_requestor);

r.setStringParameter("u_preferred_contact_number", current.u_requestor);

//r.setStringParameter("work_notes", current.work_notes.getJournalEntry(-1).toString);

var responser = r.execute();


gs.log("getBody: " + responser.getBody());

gs.log("getStatusCode: " + responser.getStatusCode());

gs.log("getHeaders " + responser.getHeaders());


Below is the log

getBody:{"error":{"message":"Exception while reading request","detail":"Verify Request body and Content-type headers. Not able to parse request"},"status":"failure"}

getStatusCode: 400

getHeaders {Date=Mon, 29 Dec 2014 03:35:50 GMT, Transfer-Encoding=chunked, Set-Cookie=BIGipServerpool_predev1bhpbio=421822474.35646.0000; path=/, X-Cnection=close, Content-Type=application/json, Server=ServiceNow}


Any ideas or inputs are highly welcomed

1 ACCEPTED SOLUTION

Hi Hetal,



Sorry about that.   The quotes are added as a result of the JSON.encode call in the previous script.   The REST message will add its own quotes with setStringParameter, hence the conflict.   I added a jsonEncode function to the script below to handle both the JSON encoding and stripping the leading and trailing quotes that are added by the encode function:



var r = new RESTMessage('TestIncidents', 'post');  



function jsonEncode(str) {


  str = new JSON().encode(str);


  return str.substring(1, str.length - 1);


}



r.setStringParameter("short_description", current.short_description);  


r.setStringParameter("u_affected_user", current.u_affected_user);  


r.setStringParameter("location", current.location);  


r.setStringParameter("impact", current.impact);  


r.setStringParameter("urgency", current.urgency);  


r.setStringParameter("description", jsonEncode(current.description + ''));  


r.setStringParameter("work_notes", jsonEncode(current.work_notes + ''));  


r.setStringParameter("u_category", current.u_category);  


r.setStringParameter("u_classification_level_3", current.u_classification_level_3);  


r.setStringParameter("service_offering", current.service_offering);  


r.setStringParameter("cmdb_ci",current.cmdb_ci);  


r.setStringParameter("u_requestor", current.u_requestor);  


r.setStringParameter("u_preferred_contact_number", current.u_requestor);  


//r.setStringParameter("work_notes", current.work_notes.getJournalEntry(-1).toString);  


var responser = r.execute();  


 


gs.log("getBody: " + responser.getBody());  


gs.log("getStatusCode: " + responser.getStatusCode());  


gs.log("getHeaders " + responser.getHeaders());  


View solution in original post

18 REPLIES 18

dalemellor
Kilo Contributor

Hi Hetal,



Good question! I've struggled with this myself.


Apparently it's not enough to escape the new line using \n alone. I've found that it does work if you escape \n (new line) AND \r (return).



I have a hunch that this will work for you:



var r = new RESTMessage('ESMIncidents', 'post');



function escapeSpecialChars(jsonString) {


      return jsonString.replace(/\n/g, "\\n")


                  .replace(/\r/g, "\\r");


}



r.setStringParameter("description", escapeSpecialChars(current.description));




Cheers,
Dale


Hi Dale,



As you indicated, escaping the newline is not enough.   However, it will likely also not be enough to escape the return either.   For instance, if you have quote marks in your string, it can cause malformed json as well.   This is due to the JSON spec which states that a string can contain any Unicode characters except the double quote, "\", or control characters.



I mention this because the actual process for JSON escaping a string is a little more complicated but thankfully is already encapsulated within the JSON script include via the encode function.   For reference, here is some helpful info on JSON formatting.


tltoulson
Kilo Sage

Hi Hetal,



You are attempting to transmit JSON, but any newline character will cause a problem if it is not properly escaped in JSON.   There is an easy way to do this:



var r = new RESTMessage('TestIncidents', 'post');


var json = new JSON();



r.setStringParameter("short_description", current.short_description);


r.setStringParameter("u_affected_user", current.u_affected_user);


r.setStringParameter("location", current.location);


r.setStringParameter("impact", current.impact);


r.setStringParameter("urgency", current.urgency);


r.setStringParameter("description", json.encode(current.description + ''));


r.setStringParameter("work_notes", json.encode(current.work_notes + ''));


r.setStringParameter("u_category", current.u_category);


r.setStringParameter("u_classification_level_3", current.u_classification_level_3);


r.setStringParameter("service_offering", current.service_offering);


r.setStringParameter("cmdb_ci",current.cmdb_ci);


r.setStringParameter("u_requestor", current.u_requestor);


r.setStringParameter("u_preferred_contact_number", current.u_requestor);


//r.setStringParameter("work_notes", current.work_notes.getJournalEntry(-1).toString);


var responser = r.execute();



gs.log("getBody: " + responser.getBody());


gs.log("getStatusCode: " + responser.getStatusCode());


gs.log("getHeaders " + responser.getHeaders());



Notice the json variable on line 2 and the use of json.encode function on lines 9 and 10.   Also notice that I coerced the current.description and current.work_notes into a string.   This is important since json.encode will encode entire objects as a string, not what you want here.


Hi Travis


Thank you for your response


I have tried what you suggested.


It works fine now but it adds string """ at the start and end of the phrase


Ex:


TEST1


TEST2


TEST3



Gets converted to


"TEST1


TEST2


TEST3"



Not sure how I can get rid of those things



Thanks again for wonderful suggestion


Regards


Hetal