The CreatorCon Call for Content is officially open! Get started here.

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

Hi Travis,

I have applied the solution you suggested, still i am getting the error messgage. Can you please help me. This is the new ticket i opened in community here -

https://community.servicenow.com/community?id=community_question&sys_id=ce7bdd2ddb7697401cd8a345ca9619c1

 

Hi,

In newer releases, you can use 

GlideStringUtil.escapeNonPrintable([string]);

Thanks,

Cody

 

It's helped me a lot, Thank you so much....

Thanks so much Travis! You're the best!