Skip to content

Instantly share code, notes, and snippets.

@bryanbarnard
Created March 8, 2016 17:51
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save bryanbarnard/1f2d9e819dfb5fad41a3 to your computer and use it in GitHub Desktop.
Save bryanbarnard/1f2d9e819dfb5fad41a3 to your computer and use it in GitHub Desktop.
sample http request in servicenow using RESTMessageV2 server side api tested running in ServiceNow Geneva Release as a background script in global scope
/**
* sample http request in servicenow using RESTMessageV2 server side api
* tested running in ServiceNow Geneva Release as a background script in global scope
*/
(function sample_http_request() {
try {
var request = new sn_ws.RESTMessageV2();
request.setHttpMethod('get');
request.setEndpoint('https://api.myjson.com/bins/4j985');
// api.myjson.com/bins/4j985 is just a static JSON data store
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);
}
})();
@mstewio
Copy link

mstewio commented May 16, 2017

Thanks for the post! What resources did you find to be most helpful when you were getting up to speed with ServiceNow development? To give you some context, I'm looking to write a custom script (Business Rule) that calls an external web service and updates ServiceNow fields based on the reply.

@mmanhaes
Copy link

Thanks for this code. It was very helpful to solve a problem I had.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment