REST API issue/question: I am getting an error stating " JSON: Unexpected character ('

kprior
Giga Contributor

Trying to run a RESST API command. When in the REST setup all is working correctly.

However, once I set up to use this from a script in the Change Workflow it is erroring.

The following script is what I get on the preview of the REST obect in the post method:

try {

var r = new sn_ws.RESTMessageV2('Nolio Deployment', 'post');

r.setStringParameter('deployment', '"CHG1111112-Fri April 29 14:00:00 CST 2015"');

r.setStringParameter('project', '"SNOWTest"');

r.setStringParameter('build', '"1.0.0"');

r.setStringParameter('application', '"Tomcat Generic"');

r.setStringParameter('environments', '["Environment for Default Architecture"]');

r.setStringParameter('deploymentPlan', '"Ken Prior"');

var response = r.execute();

var responseBody = response.getBody();

var httpStatus = response.getStatusCode();

}

catch(ex) {

var message = ex.getMessage();

}

However, this same script is failing with the following when doing this from a run script in the workflow with the following error:

{"result":false,"description":"Could not read JSON: Unexpected character ('&' (code 38)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: org.apache.catalina.connector.CoyoteInputStream@1b92a907; line: 1, column: 18]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('&' (code 38)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: org.apache.catalina.connector.CoyoteInputStream@1b92a907; line: 1, column: 18]"}

Any help would be appreciated.


1 ACCEPTED SOLUTION

kprior
Giga Contributor

I had to change my call to NoEscape in my case.



try {




  var r = new sn_ws.RESTMessageV2('Nolio Deployment', 'post');



  r.setStringParameterNoEscape('application', '"' + current.u_app + '"');


  r.setStringParameterNoEscape('build', '"' + current.u_build + '"');


  r.setStringParameterNoEscape('project', '"' + current.u_nolio_project + '"');


  r.setStringParameterNoEscape('deploymentPlan', '"' + current.u_deployment_plan + '"');


  r.setStringParameterNoEscape('environments', '["' + current.u_environments + '"]');


  r.setStringParameterNoEscape('deployment', '"' + current.u_deployment + '"');



  var response = r.execute();


  var responseBody = response.getBody();


  var httpStatus = response.getStatusCode();


  var parser = new JSONParser();


  var parsed = parser.parse(responseBody);


  current.u_nolio_results = "Result: " + parsed.result + String.fromCharCode(13) + "Description: " + parsed.description + String.fromCharCode(13);




}


catch(ex) {


  var message = ex.getMessage();


}



Thanks all.


View solution in original post

7 REPLIES 7

adam_faulkinber
Kilo Expert

I'm taking a guess here, but I think the error is with trying to add multiple string parameters.   I would try building the JSON string and running it as a parameter for execute.   Like this:



var postString = "{\"deployment\":\"CHG1111112-Fri April 29 14:00:00 CST 2015\", \"project\":\"SNOWTest\", \"build\":\"1.0.0\", \"application\":\"Tomcat Generic\",\"environments\", \"[\\"Environment for Default Architecture\\"]\", \"deploymentPlan\":\"Ken Prior\"}";



var response = r.execute(postString);




I don't have experience running REST commands from workflows, but I use this method regularly to post from external apps.  


Thanks this failed to even pass the Script checker.   :>)



Thanks for trying.


Looks like it was hitting an error when I was trying to escape the quotation marks in the value for "environments".   Here, this passes the syntax check:



var postString = "{\"deployment\":\"CHG1111112-Fri April 29 14:00:00 CST 2015\", \"project\":\"SNOWTest\", \"build\":\"1.0.0\", \"application\":\"Tomcat Generic\",\"environments\":\"[\\\"Environment for Default Architecture\\\"]\", \"deploymentPlan\":\"Ken Prior\"}";





var response = r.execute(postString);


jose_valle
ServiceNow Employee
ServiceNow Employee

Hi Kenneth,



It looks like the error is being returned form the external web service because it doesn't like what you sending. You should check what you have configured for this Outbound REST message to see what you have as the content. from the error it seems to be complaining about an "&" character



{"result":false,"description":"Could not read JSON: Unexpected character ('&' (code 38)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: org.apache.catalina.connector.CoyoteInputStream@1b92a907; line: 1, column: 18]


Hope this helps.



-Jose