- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-28-2014 11:35 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-29-2014 08:18 AM
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());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2016 02:52 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-08-2016 02:27 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-27-2016 01:41 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-19-2017 01:55 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2017 04:16 PM
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.