Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

getBody() of RESTResponseV2 always return null if have error

ducdhm
Giga Contributor

Hey guys,

I'm developing an application which interacts a lot with REST. In some case, the error will be occurred such as Authorization header is error and I want to get the response body for debugging. With previous version of RESTResponse, I can do log the response body but in V2, the body always is null if haveError() is true. Is there any work around for this?

Cheers,

Duc

1 ACCEPTED SOLUTION

ducdhm
Giga Contributor

Seems like SNC changed in getBody() method of RESTResponseV2. Now I can get the body even haveError() is true


View solution in original post

9 REPLIES 9

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Duc,



Please check section 8 in below link for more info.


REST API - ServiceNow Wiki


Hi Pradeep Sharma,


Thanks for your helping but I use Outbound REST API. So sometime REST API has error, it'll return a lot of content in response such as JSON. For troubleshooting, I need to get the response body. Is there any work around?


tony_barratt
ServiceNow Employee
ServiceNow Employee

Hi Duc,


Could you supply some more details?


You are using outbound rest api described here?


Outbound REST Web Service - ServiceNow Wiki


With or without a MID Server?


Are you on Fuji or earlier?


Towards another instance? Or something else?


You mention version V2 what does that refer to?



Best Regards



Tony


Hi Tony,


I'll explain more for you:


  1. I'm on FUJI version
  2. I use RESTMessageV2 and execute request with MID Server
  3. After I sent request to REST API endpoint, I got status code is 401. The REST API endpoint returns the JSON response when status code is 401. BUT response.getBody() is null.


FYI: The code is


// SAPI is name of REST API


var restMessage = new sn_ws.RESTMessageV2("SAPI", "get");


restMessage.setBasicAuth('myuser', 'mypassword');


restMessage.setMIDServer('MYMIDSERVER');




var response = restMessage.execute();


response.waitForResponse(120);




gs.debug('Response body: {0}', response.getBody());



FYI2: If response.hasError(), the response.getBody() always is null