Illegal access to outbound HTTP

tgra
Kilo Explorer

I am running the following code and I still get the error "Illegal access to outbound HTTP"

post: function() {

        gs.info("posting a SecureChange ticket");

        var sm = new sn_ws.RESTMessageV2();

        sm.setEndpoint(this.endpoint);

        sm.setBasicAuth(this.scUsername, this.scPassword);

        sm.setRequestHeader("Content-Type", "application/json");

        sm.setRequestHeader('Accept','application/json');

        sm.setHttpMethod("POST");

        sm.setRequestBody(new global.JSON().encode(this.scTicketTemplate));

        var response = sm.execute();

1 REPLY 1

vab_13
ServiceNow Employee
ServiceNow Employee

I recommend leverage executeAsync().


post: function() {


        gs.info("posting a SecureChange ticket");


        var sm = new sn_ws.RESTMessageV2();


        sm.setEndpoint(this.endpoint);


        sm.setBasicAuth(this.scUsername, this.scPassword);


        sm.setRequestHeader("Content-Type", "application/json");


        sm.setRequestHeader('Accept','application/json');


        sm.setHttpMethod("POST");


        sm.setRequestBody(new global.JSON().encode(this.scTicketTemplate));


        var response = sm.executeAsync();



From Unable to make async rest request from a before business rule?



When using RESTMessageV2.execute() the calling script will immediately block and wait for a response from the requested endpoint until either a response is received or the timeout as specified by the glide.http.connection_timeout property or by calling RESTMessageV2.setHttpTimeout().



When using RESTMessageV2.executeAsync() the calling script will not block and instead will move on to executing the next line in the script. However if your script references the RESTResponse object returned from calling executeAsync() and calls a method on that object (i.e., response.haveError()) then the script will block and wait either for a response to be returned or a timeout specified via either the property glide.rest.outbound.ecc_response.timeout or using RESTResponseV2.waitForResponse() to be exceeded.



Let's look at some sample scripts. The following two script samples will behave similarly. Both blocking and waiting for a response to the request or timeout before moving onto the gs.print('test test test'); line.



Execute()




  1. var request = new sn_ws.RESTMessageV2();  
  2. request.setHttpMethod('get');  
  3. request.setRequestHeader('Content-Type', 'application/json');  
  4. request.setEndpoint('http://requestb.in/vt3gi5vt');  
  5. request.execute();  
  6. gs.log('test test test');



ExecuteAsync()



  1. var request = new sn_ws.RESTMessageV2();  
  2. request.setHttpMethod('get');  
  3. request.setRequestHeader('Content-Type', 'application/json');  
  4. request.setEndpoint('http://requestb.in/vt3gi5vt');  
  5. var response = request.executeAsync();  
  6. gs.log('not blocked yet')  
  7. response.haveError();  
  8. gs.log('test test test');  


The difference is that in the ExecuteAsync sample the script will not block and wait for a response or timeout before executing line #6 (gs.print('not blocked')). However, once line #7 is executed the script will block and wait. I think there is room for improving documentation around this functionality as well as making the executeAsync functionality a bit easier to use.