RESTMessageV2 in a MID Server Script Includes

joesnow
Kilo Contributor

Hi all,

I'm in the process of creating a custom probe to identify my software using a Rest API call.

Let me know if the architecture of this seems correct or not.

  • I've created a Discovery Probe (AppDiscovery)
  • AppDiscovery creates an ECC Queue Item (Topic: JavascriptProbe, Name: AppProbe)

            with a couple parameters:

        • user
        • pass
        • script.js / var test = new AppProbe(); test.run();
  • AppProbe is a MID Server "Script Includes" (seen below):

var AppProbe= Class.create();

AppProbe.prototype = Object.extendsObject(AProbe, {

  initialize : function(probe) {

  this.probe = probe;

  },

  run : function() {

  var app_user = this.getParameter('user');

  var app_pass = this.getParameter('pass');

  ms.log('Attempting to log in with user: ' + this.getParameter('user'));

  var requestBody;

  var responseBody;

  var status;

  var sm;

   

  try{

            sm = new sn_ws.RESTMessageV2();

            sm.setEndpoint('https://mysoftware.com/authentication');

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

            sm.setHttpMethod('post');  

            sm.setRequestBody('{"username":' + app_user + ',"password":' + app_pass + '}');

            response = sm.execute();

            responseBody = response.haveError() ? response.getErrorMessage() : response.getBody();

            status = response.getStatusCode();

  } catch(ex) {

            responseBody = ex.getMessage();

            status = '500';

  } finally {

            requestBody = sm ? sm.getRequestBody():null;

  }

  ms.log("Request Body: " + requestBody);

  ms.log("Response: " + responseBody);

  ms.log("HTTP Status: " + status);

},

  type: "AppProbe"

});

When I execute the AppDiscovery against my MID Server, these are my logs:

12/06/16 15:48:47 (636) Worker-Interactive:JavascriptProbe Worker starting: JavascriptProbe

12/06/16 15:48:47 (637) Worker-Interactive:JavascriptProbe *** Script: Attempting to log in with user: test

12/06/16 15:48:47 (769) Worker-Interactive:JavascriptProbe *** Script: Response: undefined

12/06/16 15:48:47 (769) Worker-Interactive:JavascriptProbe *** Script: Request Body: null

12/06/16 15:48:47 (769) Worker-Interactive:JavascriptProbe *** Script: Response: undefined

12/06/16 15:48:47 (769) Worker-Interactive:JavascriptProbe *** Script: HTTP Status: 500

Upon removing the try/catch, I get the source of the "nulls".

"sn_ws" is not defined

I'm able to run this script without issue as a "Background script", but for some reason I can't run it as a MID Server Script Includes (which is where I think it should be run..)

Any advice or suggestions on how to execute a Rest call inside this script?

Thanks,

-Joe

3 REPLIES 3

alexbin
Kilo Contributor

Hi All!



I have the same problem. Can anybody help me with this?



Regards,


congthieu
ServiceNow Employee
ServiceNow Employee

You don't need to create a custom probe. RESTMessageV2 runs on the instance and can make a REST call directly or via a MID server.



        http://wiki.servicenow.com/index.php?title=Scripting_Outbound_REST#gsc.tab=0



In a workflow you can put the code inside a run script activity.



Regards,



Cong


Hi



Yes, you are right, it's possible to make REST call via MID server (http://wiki.servicenow.com/index.php?title=RESTMessageV2_API#setMIDServer.28String_midServer.29 ).



But I really need to do this in MID server script include for some reason.



I found the solution. Yes, RESTMessageV2 can't be used in the MID server script include, but we can use GlideHTTPCient instead.



var GetMethod = Packages.org.apache.commons.httpclient.methods.GetMethod;


var GlideHTTPCient = Packages.com.glide.communications.HTTPClient;


var httpClient = new GlideHTTPCient();




var getMethod = new GetMethod('https://...'); // put your endpoint URL here


getMethod.addRequestHeader('X-Auth-Token', '...'); // add request headers




var httpStatus = httpClient.executeMethod(getMethod);


var response = getMethod.getResponseBodyAsString();


getMethod.releaseConnection();



You can also see discovery MID server script include 'UCSRestXmlApiClient', it uses GlideHTTPClient and POST method.