top of page

Experiencing challenges with ServiceNow support?

Access professional, reliable assistance tailored to your needs—without breaking your budget.

How to Pass Values with New Line and Special Characters in ServiceNow REST APIs

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.

Experiencing challenges with ServiceNow support?

Access professional, reliable assistance tailored to your needs—without breaking your budget.

CONTACT

New Zealand HQ

Integrated Knowledge Consulting Office

Level 3, 93 Grafton Road

Auckland

South Korea

Integrated Knowledge Consulting Office

BMY역삼타워 6층

서울특별시 강남구 역삼동 678-10번지

 

info@ikconsulting.com

Thanks for submitting!

  • LinkedIn Social Icon

© Copyright 2025 Integrated Knowledge Consulting. All rights reserved.

bottom of page