Sending quotes in REST api request
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2023 04:20 PM
My requirement is to send the full text including any special characters from Incident task description in the json request. json Essentially, I need to escape any special character.
for example, if Description is - This is "Test"
then it should be sent like this in the json request - This is a \”Test\”
How do I escape the special characters while setting up the request parameter for the json request
I tried setStringParameterNoEscape but in that case, the description in json request is - This is "Test"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2023 12:01 AM - edited 02-01-2023 12:02 AM
Try below:
var myJSONString = JSON.stringify(myJSON);
var myEscapedJSONString = myJSONString.replace(/\\n/g, "\\n")
.replace(/\\'/g, "\\'")
.replace(/\\"/g, '\\"')
.replace(/\\&/g, "\\&")
.replace(/\\r/g, "\\r")
.replace(/\\t/g, "\\t")
.replace(/\\b/g, "\\b")
.replace(/\\f/g, "\\f");
Please mark the answer correct/helpful accordingly.
Raghav
MVP 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2023 02:15 AM
try to replace the double quote with html encoding
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2023 09:32 AM - edited 02-01-2023 12:43 PM
I was able to use regex to do what is needed for quotes - replace(/"/g, '\\"');
However, this doesn't work for escaping \
I need to send it like \\
Any pointers?
Also, how do I escape backspace character? Regex treats \b are word break - I want to escape backspace character (hex character code 08). If i use \x0B, business rule script doesn't accept it and treats it as unexpected.