REST message

asham2
Kilo Contributor

Hi,

I have created a BR for integrating two instance using REST messages, if I create a incident in source   instance incident it should get created in target instance incident, but i got an error like this,

Inserted Incident through REST : 400 {"error":{"detail":"Cannod decode: java.io.StringReader@1e2a587","message":"Exception while reading request"},"status":"failure"}

Can anyone please give me the solution to solve this error?

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Asha,



I think you must be using the post method to create a new incident in target instance.


Just test it first from the rest message.


I think this error is something to do with the data which you are sending through post message. Can you share the script which calls the rest message. Also the screenshot of the rest message, rest message post function and the content block for post message.



Regards


Ankur


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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Asha,



Were you able to achieve your requirement? If yes then please mark the answer as correct, helpful and hit like. This helps in closing the thread for the question. Thanks in advance.



Regards


Ankur


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

jayw
ServiceNow Employee
ServiceNow Employee

Hi Asha ( asham2,



I actually just ran into this issue while using the browser's built-in javascript class, XmlHttpRequest() and PUT method, to update a record in a custom table. While I can't say for certain what your issue is since you didn't share all of the information that Ankur requested, I was able to resolve by using JSON.stringify() to convert the data before sending.



This is something that I wouldn't expect on other systems, since I added the 'Content-Type: application/json' request header to my request, so I suspect others new to ServiceNow may run into this issue if using lower-level/legacy methods like REST messages or XmlHttpRequest. If you are making REST requests from angular's $http service, the payload seems to be converted/decoded as expected, so you shouldn't need to stringify the payload of angular $http requests. I would always recommend using angular from now on, but I am presently bridging the gap between some older applications on our instance and newer ones, so angular isn't always an option.



In the future, when asking the Community (or other resources like stackoverflow) for help on REST calls, other developers will typically need the following information to best help you:



1) the data that you are sending (request payload/parameters)


2) the code that you are using to send the request


3) the response (which you provided)



It's generally best to include as much information as possible in the original request as we can't see your code. If this was helpful, please mark as correct and like.



- Jay Wigard, Senior UI Engineer


My code is below.   It was working, then suddenly I started getting 400 errors.   Is it possible the other instance has had a new business rule put in place that might cause this to happen?   Maybe set a new field as required?   The CS record does get created, but the return is a 400 instead of a 201.



Marshall



function escapeSpecialChars(strng) {


      return strng.replace(/\\n/g, "\\n")


                          .replace(/\\'/g, "\\'")


                          .replace(/\\"/g, '\\"')


                          .replace(/\\&/g, "\\&")


                          .replace(/\\r/g, "\\r")


                          .replace(/\\t/g, "\\t")


                          .replace(/\\b/g, "\\b")


                          .replace(/\\f/g, "\\f");


}



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


   


   


      try {


              var r = new sn_ws.RESTMessageV2('PushToSEI', 'CreateNewCS');


              r.setStringParameterNoEscape('short_description', escapeSpecialChars(current.short_description));


              r.setStringParameterNoEscape('number', current.number.toString());


              r.setStringParameterNoEscape('assignment_group', 'Dispatch');


              r.setStringParameterNoEscape('description', escapeSpecialChars(current.description));


              r.setStringParameterNoEscape('comments', escapeSpecialChars(current.comments));


              r.setStringParameterNoEscape('state', 'New');


              r.setStringParameterNoEscape('sys_id', current.sys_id);


              r.setStringParameterNoEscape('account', current.u_store.getDisplayValue());


           


              //override authentication profile


              //authentication type ='basic'/ 'oauth2'


              //r.setAuthentication(authentication type, profile name);


           


              var response = r.execute();


              var requestBody = r.getRequestBody();


              var responseBody = response.getBody();


              var httpStatus = response.getStatusCode();


           


              current.work_notes = requestBody + "\r\n" + httpStatus + ": " + responseBody;


              //gs.addInfoMessage("httpStatus="+httpStatus+"\r\nresponseBody="+responseBody);


           


              var json = JSON.parse(responseBody);


              current.correlation_id = json.result.sys_id;


              current.vendor_ticket = json.result.number;


              current.update();


      } catch(ex) {


              var message = ex.getMessage();


              gs.addErrorMessage(message);


      }


   


})(current, previous);