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

Actually this exact data elements works just fine in the REST test message per the REST configuration, just not from the Run Script.



The parameters have been checked and re-checked at let 20 times, and tested with the apostrophe and without, as well as the Quote marks. But they are the same as the preview script data from the REST setup page as well.



Without the apostrophe and quote marks the error is diferrent error.



Thanks though.


kprior
Giga Contributor

The parameters have been checked and re-checked at let 20 times, and tested with the apostrophe and without, as well as the Quote marks. But they are the same as the preview script data from the REST setup page as well.



Without the apostrophe and quote marks the error is diferrent error.



{"result":false,"description":"Could not read JSON: Unrecognized token 'Tomcat': was expecting ('true', 'false' or 'null')\n at [Source: org.apache.catalina.connector.CoyoteInputStream@8775e8a; line: 1, column: 24]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'Tomcat': was expecting ('true', 'false' or 'null')\n at [Source: org.apache.catalina.connector.CoyoteInputStream@8775e8a; line: 1, column: 24]"}



Essentially it appears that when removing the apostrophe or quote marks it does not get the formatted data as expected. The service I am sending this to expects the quotes around the data as part of the string sent, and by removing one or the other it does not see these. it is passed as a string without the quotes in the data.



With the apostrophe and quote marks then I get the first error above.



Hope this clarifies it a little. I need to pass a string including quotes as such: "Tomcat Generic" to the receiving system. It expects a value that is in quotes so I need to pass the string to look as it is here.



Thanks to all who have attempted to help so far and those brave souls who will continue!


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.