Outbound Rest Message using get method

salu
Mega Guru

Hello,

I have used below background script to test the functionality and getting the below error.

What need to be checked from the endpoint url?

(function sample_http_request() {

      try {

              var request = new sn_ws.RESTMessageV2();

              request.setHttpMethod('get');

              request.setEndpoint('http://localhost:8080/xxx?id=1234');

              var response = request.execute();

              var httpResponseStatus = response.getStatusCode();

              var httpResponseContentType = response.getHeader('Content-Type');

              var parser = new global.JSONParser();

              var parsed = {};

              var httpResponseBody;

              gs.debug("http response status_code: " + httpResponseStatus);

              gs.debug("http response content-type: " + httpResponseContentType);

              //   if request is successful then parse the response body

              if (httpResponseStatus == 200 && httpResponseContentType == 'application/json') {

                      httpResponseBody = response.getBody();

                      //   parse JSON string returned from request into a json object

                      parsed = parser.parse(httpResponseBody);

                      // iterate over JSON object only printing the id property of JSON objects in results array

                      for (var i = 0; i < parsed.results.length; i++) {

                              gs.print('id: ' + parsed.results[i].id)

                      }

              }

      }

      catch (ex) {

              var message = ex.getMessage();

              gs.debug(message);

      }

})();

find_real_file.png

7 REPLIES 7

Paramahanns
Tera Expert

Hi,



Please find this code. You can try running this.



var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://test.service-now.com/api/now/table/incident?sysparm_fields=number&sysparm_limit=1');
request.setHttpMethod('GET');


var user = 'admin';
var password = 'admin';


request.setBasicAuth(user,password);
request.setRequestHeader("Accept","application/json");

var response = request.execute();
gs.log(response.getBody());
var httpResponseStatus = response.getStatusCode();
gs.log(httpResponseStatus);



You can modify the code accordingly and see if it works.


rami1219
Kilo Expert

Hi saranya babu,



Instead of this end point       request.setEndpoint('http://localhost:8080/xxx?id=1234');   please put this     request.setEndpoint(give your instance endpoint);



Please click helpful,correct answer,like



Thanks,


Raminaidu


Hello Avinash,



I have used my dev url and getting below error


find_real_file.png


Hello Avinash.



I have used the below code and am getting the result.



What might be the my issue?In the that thirdparty url access need   be checked?




var request = new sn_ws.RESTMessageV2();


request.setEndpoint('https://sample.service-now.com/api/now/table/incident?sysparm_fields=number&sysparm_limit=10');


request.setHttpMethod('GET');


var user = 'userid';


var password = 'paswrd';


request.setBasicAuth(user,password);


request.setRequestHeader("Accept","application/json");




var response = request.execute();


gs.log(response.getBody());


var httpResponseStatus = response.getStatusCode();


gs.log(httpResponseStatus);



[0:00:06.570] Script completed in scope global: script



*** Script: {"result":[{"number":"INC0010001"},{"number":"INC0010002"},{"number":"INC0010003"},{"number":"INC0010004"},{"number":"INC0010005"},{"number":"INC0010006"},{"number":"INC0010007"},{"number":"INC0010008"},{"number":"INC0010009"},{"number":"INC0010010"}]}
*** Script: 200