How to change to JSON stringify : Cannot decode: java.io.StringReader

nomadie
Kilo Expert

I have this Business Rule for Rest Message Patch method that I need to fix because I keep having this error.

HTTP Status : {"error":{"detail":"Cannot decode: java.io.StringReader@70dd85","message":"Exception while reading request"},"status":"failure"}

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

	// Condition is : current.correlation_idISNOTEMPTY
	gs.log('In Business Rule New Run', 'check');
	try {
		var comment = current.comments.getJournalEntry(1);
		gs.log('Comment : ' + comment, 'check');
		gs.log('Cor ID : ' + current.correlation_id, 'check');
		var r = new sn_ws.RESTMessageV2('Ebond', 'Patch Ebond');
		r.setStringParameterNoEscape('sys_id', current.correlation_id);
		r.setStringParameterNoEscape('comments',comment);
				
		var response = r.execute();
		var responseBody = response.getBody();
		var httpStatus = response.getStatusCode();
		gs.log('HTTP Status : ' + responseBody,'check');
		
	}
	
	catch(ex) {
		var message = ex.message;
	}
	
	gs.log("End Business Rule", 'check');
	
})(current, previous);
5 REPLIES 5

vkachineni
Kilo Sage
Kilo Sage

That error message could be coming as a response from the API. May be an error occurred on the API side.

Can you test with Postman?

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

how to do that? this is my first attempt in using rest. thanks

Hi,

Can you check what request body is being sent out?

Have you defined the variable substitutions properly?

gs.info('Request Body is: ' + r.getRequestBody());

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

DScroggins
Kilo Sage

Hello,

 

That error is coming from your catch statement here:

 

catch(ex) {
	var message = ex.message;
}

 

I have found that simply using the following will work and will capture any errors when attempting to execute REST calls:

catch(ex) {
  var message = ex;
  gs.error('Unexpected error while executing REST message' + ex);
}

 

Also make sure that you have the correct REST parameter variables (sys_id & comments) defined like so:

find_real_file.png

See the following for more information on REST variable substitution: Variable substitution in outbound REST messages 

 

Hope this helps.

 

--David