REST Script help- The payload is not valid JSON

CandyDee
Kilo Sage

Afternoon all 

 

Hoping someone with a bit more coding/scripting chops could point our where I can fix my issue. So I have a rest api call that is sending Incident data when incident is created to another customer instance. Its working fine when 'Short description' and 'Description' has plain text in there but its failing when someone pastes this text into the description.

 

Thanks all

 

Business rule script-

 

(function executeRule(current, previous /*null when async*/ ) {

function jsonEncode(str) {
str = new JSON().encode(str);
return str.substring(1, str.length - 1);
}

try {
var r = new sn_ws.RESTMessageV2('Integration', 'Create Incident');
var desc = current.description.replace(/(\r\n|\n|\r)/gm, ",");
var ci_loc = current.cmdb_ci.location;

r.setStringParameterNoEscape('desc', desc); 
//r.setStringParameterNoEscape('desc', jsonEncode(current.description + '')); 
r.setStringParameterNoEscape('short_desc', current.short_description);
r.setStringParameterNoEscape('comments', current.comments);
r.setStringParameterNoEscape('cor_id', current.sys_id);
r.setStringParameterNoEscape('cust_ref', current.number);
r.setStringParameterNoEscape('priority', current.priority);
//r.setStringParameterNoEscape('notes', current.work_notes); -
r.setStringParameterNoEscape('ci', current.cmdb_ci.getDisplayValue());

 

var response = r.execute();
var responseBody = response.getBody();

var obj = JSON.parse(responseBody); // define JSON parsing for reponse JSON file to decode

var foundsysID = obj.result.sys_target_sys_id.value; //decoded newly created incident sys_id
current.correlation_id = foundsysID; //copied the number of 3rd incident to correlation id

var number = obj.result.u_number; // decoded 3rd party newly created incident number
current.u_customer_ref = number;

var httpStatus = response.getStatusCode();
gs.log('>>>>> Create Incident BR = ' + httpStatus + ' - ' + responseBody);
} catch (ex) {
var message = ex.message;
}
})(current, previous);

 

Text copied into the descripton field via servicedesk which causes a failure in the logs of '400 - {"error":{"message":"Exception while reading request","detail":"The payload is not valid JSON."},"status":"failure"}

 

The following vendors have released security updates to address multiple vulnerabilities:
Palo Alto Networks Security Advisory CC-4191
Ivanti/Pulse Secure Security Advisory CC-4193
Oracle Critical Patch Update Advisory CC-4194
Cisco Security Advisories CC-4195
Adobe Security Bulletin CC-4196
F5 Security Advisory CC-4197

Affected organisations are encouraged to review the following vendor security advisories and apply any relevant updates or mitigations:
Palo Alto Networks Security Advisory CVE-2022-0030
Ivanti's Pulse Secure Security Update SA45520
Oracle October 2022 Critical Patch Update Advisory
Cisco Security Advisories:
cisco-sa-meraki-mx-vpn-dos-vnESbgBf
cisco-sa-ise-path-trav-Dz5dpzyM
cisco-sa-jabber-xmpp-Ne9SCM
cisco-sa-roomos-trav-beFvCcyu
cisco-sa-ise-xss-twLnpy3M
Adobe Security Updates: Illustrator | APSB22-56
F5 Security Advisory K30425568: Overview of F5 vulnerabilities (October 2022)

 

 

1 ACCEPTED SOLUTION

CandyDee
Kilo Sage

Seemed to of got it working by doing this-

r.setStringParameterNoEscape('desc', jsonEncode(current.description + ''));

View solution in original post

6 REPLIES 6

CandyDee
Kilo Sage

Seemed to of got it working by doing this-

r.setStringParameterNoEscape('desc', jsonEncode(current.description + ''));

anandk222
Tera Contributor

Hi,

I was facing the same issue, following fixed it for me.
While creating HTTP POST Method, in the content instead of using dynamic variables directly put them inside double quotes.
Example:
{
"short_description":"${sd}",
"description":"${desc}",
"caller_id":"${caller}"
}.

This will work