diff --git a/Server-Side Components/Script Includes/RestMessageUtils/README.md b/Server-Side Components/Script Includes/RestMessageUtils/README.md new file mode 100644 index 0000000000..4cd53d4ce3 --- /dev/null +++ b/Server-Side Components/Script Includes/RestMessageUtils/README.md @@ -0,0 +1,62 @@ +RestMessageUtils +A utility Script Include for simplifying REST API calls in ServiceNow using sn_ws.RESTMessageV2. +Features + +Supports dynamic or named REST messages. +Easily set headers, query parameters, and request body. +Supports Basic Auth, Auth Profiles, and API Key authentication. +Optional MID Server configuration. +Handles variable substitution. +Centralized error handling with gs.error() logging. +Designed for use in Script Includes, Business Rules, or Scripted REST APIs. +Lightweight and reusable for multiple integrations. + +Object Structure +/* +ObjectStructure = { + endPoint: 'enpoint', + httpMethod: 'Add Method', + queryParams: { + key1: 'value', + key2: 'value' + }, + requestHeaders: { + }, + authType: 'CHOICES among [BasicCreds,BasicAuthProfile,APIKEY]', + authProfile: 'sysid of auth profile if authtype is selected as BasicAuthProfile', + userName: 'userName', + pasword: 'password', + midServer: 'midServer', + +} +*/ + +Example Usage +/* +var obj = { + endPoint: 'https://instancename.service-now.com/api/now/table/problem', + queryParams: { + sysparm_query: 'active=true', + sysparm_limit: 2, + sysparm_fields: 'number,short_description' + }, + httpMethod: 'POST', + authType: 'BasicCreds', + userName: 'admin', + password: gs.getProperty('dev204127.admin.password'), + requestHeaders: { + Accept: 'Application/JSON' + }, + + +}; +var resp = new RestMessageUtils(obj, 'Test RestMessage Utils', 'Default GET').execute(); + +gs.print(resp.getBody()) +*/ + + + + + + diff --git a/Server-Side Components/Script Includes/RestMessageUtils/RestMessageUtils.js b/Server-Side Components/Script Includes/RestMessageUtils/RestMessageUtils.js new file mode 100644 index 0000000000..18d80fb504 --- /dev/null +++ b/Server-Side Components/Script Includes/RestMessageUtils/RestMessageUtils.js @@ -0,0 +1,85 @@ +var RestMessageUtils = Class.create(); +RestMessageUtils.prototype = { + initialize: function(rmObj, restMessage, restFunc) { + this.RM = (restFunc && restMessage) ? new sn_ws.RESTMessageV2(restMessage, restFunc) : new sn_ws.RESTMessageV2(); + this.rmObj = rmObj; + }, + + checkAndGetKeyValue: function(obj, key) { + return obj.hasOwnProperty(key); + + }, + + setQueryParams: function() { + if (this.checkAndGetKeyValue(this.rmObj, 'queryParams')) + for (var i in this.rmObj.queryParams) + this.RM.setQueryParameter(i, this.rmObj.queryParams[i]); + + }, + + variableSubs: function() { + if (this.checkAndGetKeyValue(this.rmObj, 'variableInfo')) + for (var i in this.rmObj.variableInfo) + this.RM.setStringParameterNoEscape(i, this.rmObj.variableInfo[i]); + }, + + setAuth: function() { + var authType = this.rmObj.authType; + + if (authType) { + if (authType == 'APIKEY') + return; + else if (authType == 'BasicCreds') + this.RM.setBasicAuth(this.rmObj.userName, this.rmObj.password); + else if (authType == 'BasicAuthProfile') + this.RM.setAuthenticationProfile('basic', this.rmObj.authProfile); + + } + + }, + + setRequestBody: function() { + if (this.checkAndGetKeyValue(this.rmObj, 'requestBody')) + this.RM.setRequestBody(typeof this.rmObj.requestBody == 'object' ? JSON.stringify(this.rmObj.requestBody) : this.rmObj.requestBody); + }, + + setRequestHeaders: function() { + if (this.checkAndGetKeyValue(this.rmObj, 'requestHeaders')) + for (var i in this.rmObj.requestHeaders) + this.RM.setRequestHeader(i, this.rmObj.requestHeaders[i]); + }, + + setMidServer: function() { + if (this.checkAndGetKeyValue(this.rmObj, 'midServer')) + this.RM.setMidServer(this.rmObj.midServer); + }, + + setEndpoint: function() { + if (this.checkAndGetKeyValue(this.rmObj, 'endPoint')) + this.RM.setEndpoint(this.rmObj.endPoint); + }, + setHttpMethod: function() { + if (this.checkAndGetKeyValue(this.rmObj, 'httpMethod')) + this.RM.setHttpMethod(this.rmObj.httpMethod); + }, + + execute: function() { + try { + this.setHttpMethod(); + this.setEndpoint(); + this.setRequestHeaders(); + this.setAuth(); + this.setRequestBody(); + this.setQueryParams(); + this.variableSubs(); + this.setMidServer(); + return this.RM.execute(); + } catch (err) { + gs.error('REST Message execution failed: ' + err); + } + + }, + + + type: 'RestMessageUtils' +};