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 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());  


Hi Travis


It worked perfect!


Thank you so much for prompt responses


Regards


Hetal


Hi i have tried the above code, it was worked perfectly while getting response from REST.   But while creating SOAP response it is not working.



My issue is While getting response from external system, Description filed will come with \n or \r characters. I have tried the JSON() code, it was worked for REST but not for SOAP.



if (!this._isEmpty(this.description)){


  var xDesc1 = this.description;


  xDesc = xDesc1.replace(/\n/g, '\n');


  }


I have tried the above code, but it wont worked out.



Can anybody resolve this asap?


Thanks in Advance


gsnow
Kilo Contributor

I tried with the below code. Its working now.



if (!this._isEmpty(this.description)){


                                                                      xDesc = this.description.replace(/\\n|\\r\\n|\\r/gm, '\n');


}


Hi Travis,



We have used the above code but getting below error:


{"errorMessages":["Unrecognized character escape '&' (code 38)\n at [Source: org.apache.catalina.connector.CoyoteInputStream@71134d; line: 8, column: 240]"]}



Looks like it is not replacing the '&' character, Can you please help ?