How to Pass Values with New Line and Special Characters in ServiceNow REST APIs
- ericpark68
- Mar 21
- 3 min read
Updated: Mar 30

ServiceNow users frequently encounter scenarios where incidents must be created or updated in one ServiceNow instance using data from another instance through REST APIs. A common challenge arises when fields like "description" and "work notes" contain new lines or special characters, resulting in errors and failed incident creation.
In this article, we’ll explore why this issue occurs, provide clear troubleshooting solutions with step-by-step examples, and highlight alternative approaches to successfully pass complex text fields between ServiceNow instances.
Understanding the Error: Why New Lines and Special Characters Cause Issues
When passing text values containing new line (\n) or carriage return (\r) characters through ServiceNow REST APIs, you might encounter an error similar to:
{"error":{"message":"Exception while reading request","detail":"Verify Request body and Content-type headers. Not able to parse request"},"status":"failure"}
This error (HTTP 400) typically occurs because REST API messages expect properly encoded JSON, and any raw new line or special characters can invalidate JSON formatting.
Common Causes of the Error:
New line characters (\n) and carriage returns (\r) not escaped properly.
Special characters (e.g., quotes, ampersands) causing conflicts with JSON parsing.
Effective Solutions for Handling New Lines and Special Characters
Below are two verified and reliable methods to resolve this problem:
Solution 1: Using a Custom JSON Encode Function
This approach encodes the values correctly into JSON format and ensures that extra quotes or special characters do not disrupt the REST API parsing.
Here’s a step-by-step script you can use:
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("description", jsonEncode(current.description + ''));
r.setStringParameter("work_notes", jsonEncode(current.work_notes + ''));
var response = r.execute();
gs.log("Response Body: " + response.getBIn newer ServiceNow releases, the built-in method GlideStringUtil.escapeNonPrintable() can help escape non-printable characters, though it may not handle all newline or carriage return cases depending on the version:ody());
gs.log("Status Code: " + response.getStatusCode());
This method addresses both the JSON encoding and removes unwanted additional quotes, resolving the parsing errors.
Solution 2: Escaping New Line and Carriage Return Characters
Another effective method involves explicitly escaping new line and carriage return characters:
var r = new RESTMessage('ESMIncidents', 'post');
function escapeSpecialChars(text) {
return text.replace(/\n/g, "\\n")
.replace(/\r/g, "\\r");
}
r.setStringParameter("description", escapeSpecialChars(current.description));
r.setStringParameter("work_notes", escapeSpecialChars(current.work_notes));
var response = r.execute();
gs.log(response.getBody());
Alternative Method: Using Built-in ServiceNow Utilities
In newer ServiceNow releases, the built-in method GlideStringUtil.escapeNonPrintable() can help escape non-printable characters, though it may not handle all newline or carriage return cases depending on the version:
var escapedDescription = GlideStringUtil.escapeNonPrintable(current.description);
r.setStringParameter("description", escapedDescription);
Practical Use Cases
These solutions are practical for integrating ServiceNow instances or other external systems, ensuring reliable data exchange regardless of content complexity. Especially relevant scenarios include automated incident creation between production and test environments or between organizations.
Conclusion
When transmitting data containing special characters or new lines via REST APIs in ServiceNow, using appropriate encoding or escaping methods is crucial. Whether you prefer custom JSON encoding, explicit character escaping, or built-in utilities like GlideStringUtil, each approach ensures your data reaches its destination accurately without errors.
Next Steps:
Evaluate your integration use case to select the most suitable solution from the methods outlined.
Test thoroughly after implementation to ensure stability.
Monitor logs to proactively identify and address any edge cases that may arise.
By following these guidelines, your ServiceNow integrations will become more reliable, secure, and efficient, saving valuable time and minimizing disruptions in your workflows.