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 Sachin,



Are you attempting to use this in REST or SOAP?   REST shouldn't require any character escaping except for backslash and quote marks if I remember correctly.   If you are dealing with SOAP or XML in any way, then this would definitely make sense as you would need to XML encode any strings.   That would make more sense because ampersand is notoriously problematic in XML documents.   Let me know and if you need some additional info on this, I will see if I can dig something up.   I want to say the Jelly docs have some info about XML escaping that might be useful.



Kind regards,


Travis


Hi Travis,



I am using it in REST.



The weird thing is that it has failed only twice and I am not able to reproduce the issue


Hi Sachin,



I am using REST API and also getting the same error even though when i removed '&' from the fields.


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


I was wondering if you got solution for your issue.



Thanks for the help in advance.



Gaurav



Hi Guarav,



I am using REST API and getting the same error, kindly let me know how you have fixed the issue?


Thanks in advance.


Hey Kumaran - If you haven't found a solution yet check out the following thread: Re: how to pass & in an outbound rest message



Basically you need to use   setStringParameterNoEscape.