Skip to content

Instantly share code, notes, and snippets.

@bryanbarnard
Last active April 14, 2016 23:08
Show Gist options
  • Save bryanbarnard/60ce6cdccc67e333005cf62cc07b3c5b to your computer and use it in GitHub Desktop.
Save bryanbarnard/60ce6cdccc67e333005cf62cc07b3c5b to your computer and use it in GitHub Desktop.
Sample Outbound HTTP Request with RESTMessageV2 - Scripted
/**
* sample http request using RESTMessageV2 server side api
*/
(function sample_http_request() {
try {
var request = new sn_ws.RESTMessageV2();
request.setHttpMethod('get');
request.setEndpoint('https://api.myjson.com/bins/4j985');
var response = request.execute();
var httpResponseStatus = response.getStatusCode();
var httpResponseContentType = response.getHeader('Content-Type');
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 = new global.JSON().decode(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.debug('id: ' + parsed.results[i].id)
}
}
}
catch (ex) {
var message = ex.getMessage();
gs.debug(message);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment