How to avoid escaping of JSON in REST message POST call?

mg85
Kilo Contributor

There is a REST message defined in Service Now.

The POST call in the REST message has a variable ${content} in its content field.

And a REST Message Function Parameter called "content" is defined with a valid JSON as string:

 

{"result":{"upon_approval":"proceed","reason":"",................................

 

But, testing this service shows that all quotes in the JSON above are escaped. Input content sent to the REST Message call looks like this:

 

{"result":{"upon_approval&quot.................................

 

Is there a way to avoid this auto escape? I faced the same problem when I tried to call this REST message from a script too.


Some of the options I tried in the script:

JSONParser(), JSON.encode()

 

Message was edited by: M G

1 ACCEPTED SOLUTION

Jamie_douglas
Giga Expert

Hey,



I have checked a REST message that uses JSON in my system and here is a snippet, maybe this will help. This posts incidents to Jira..



So we start but running a business rule and create an object...


var issue = new Object();


issue.fields = new Object();


issue.fields.project = new Object();


issue.fields.project.key = gs.getProperty("com.snc.integration.jira.project");


issue.fields.summary = ""+current.short_description;


issue.fields.issuetype = new Object();


issue.fields.customfield_10025 = ""+current.number;


issue.fields.issuetype.name=""+current.subcategory;


issue.fields['customfield_'+j.sysidField] = j.getSysIdPrefix()+":"+current.sys_id;


issue.fields.priority = new Object();


issue.fields.priority.id = j.getJiraPriority(""+currentPriority);


if(j.verbose == "true"){


  JSUtil.logObject(issue);


}



We then pass that object into a script include and the function to create in incident via rest message is below..



var jiraIssue = j.createIssue(issue);



              var json = new JSON();


              var body = json.encode(issue);


              var r = new RESTMessage('Jira Issue', 'post');


              r.setBasicAuth(gs.getProperty('com.snc.integration.jira.jira_api_user'), gs.getProperty('com.snc.integration.jira.jira_api_password'));


              r.setXMLParameter('issuebody', body);


              r.setStringParameter('base_endpoint', gs.getProperty('com.snc.integration.jira.base_jira_instance_url'));


              var res = this._submitBodyTypeRequest(r, true);


              return res;



Let me know if this helps with your troubles?


View solution in original post

5 REPLIES 5

Jamie_douglas
Giga Expert

Hey,



I have checked a REST message that uses JSON in my system and here is a snippet, maybe this will help. This posts incidents to Jira..



So we start but running a business rule and create an object...


var issue = new Object();


issue.fields = new Object();


issue.fields.project = new Object();


issue.fields.project.key = gs.getProperty("com.snc.integration.jira.project");


issue.fields.summary = ""+current.short_description;


issue.fields.issuetype = new Object();


issue.fields.customfield_10025 = ""+current.number;


issue.fields.issuetype.name=""+current.subcategory;


issue.fields['customfield_'+j.sysidField] = j.getSysIdPrefix()+":"+current.sys_id;


issue.fields.priority = new Object();


issue.fields.priority.id = j.getJiraPriority(""+currentPriority);


if(j.verbose == "true"){


  JSUtil.logObject(issue);


}



We then pass that object into a script include and the function to create in incident via rest message is below..



var jiraIssue = j.createIssue(issue);



              var json = new JSON();


              var body = json.encode(issue);


              var r = new RESTMessage('Jira Issue', 'post');


              r.setBasicAuth(gs.getProperty('com.snc.integration.jira.jira_api_user'), gs.getProperty('com.snc.integration.jira.jira_api_password'));


              r.setXMLParameter('issuebody', body);


              r.setStringParameter('base_endpoint', gs.getProperty('com.snc.integration.jira.base_jira_instance_url'));


              var res = this._submitBodyTypeRequest(r, true);


              return res;



Let me know if this helps with your troubles?


Using setXMLParameter to send the JSON instead of setStringParameter did the trick. Thank you!


Thank you for the helpful post MG.   Using setXMLParameter solved my issue, and I don't know how else I would have found this.


When using RESTMessageV2, call setStringParameterNoEscape() instead. The method setXMLParameter does'nt seem to work.