Outbound REST Calls in ServiceNow: RESTMessageV2 Complete Guide

RESTMessageV2 is the API you use for scripted outbound HTTP calls from ServiceNow. Here is the complete reference for making calls, handling responses, and managing errors properly.

Basic GET request

var rm = new sn_ws.RESTMessageV2();
rm.setHttpMethod('GET');
rm.setEndpoint('https://api.example.com/v1/users');
rm.setRequestHeader('Authorization', 'Bearer ' + token);
rm.setRequestHeader('Content-Type', 'application/json');

var response = rm.execute();
var statusCode = response.getStatusCode();
var body = response.getBody();

if (statusCode == 200) {
    var data = JSON.parse(body);
    gs.log('Users returned: ' + data.users.length);
} else {
    gs.error('API call failed: ' + statusCode + ' - ' + body);
}

POST request with JSON body

var rm = new sn_ws.RESTMessageV2();
rm.setHttpMethod('POST');
rm.setEndpoint('https://api.example.com/v1/incidents');
rm.setRequestHeader('Content-Type', 'application/json');
rm.setRequestHeader('Authorization', 'Bearer ' + token);

var payload = {
    title: current.getValue('short_description'),
    severity: current.getValue('urgency'),
    reference: current.getValue('number')
};
rm.setRequestBody(JSON.stringify(payload));

var response = rm.execute();
var statusCode = response.getStatusCode();

Using a named REST Message

For reusable configurations, create a REST Message record at System Web Services > Outbound > REST Message. Define base URL, authentication, and HTTP methods. Then call it by name:

var rm = new sn_ws.RESTMessageV2('My External API', 'Create Incident');
rm.setStringParameterNoEscape('short_description', current.getValue('short_description'));
var response = rm.execute();

Asynchronous execution

// Returns immediately, response handled via callback
rm.executeAsync(callbackFunctionName);

Error handling

try {
    var response = rm.execute();
    if (response.getStatusCode() >= 400) {
        gs.error('HTTP error: ' + response.getStatusCode());
    }
} catch (ex) {
    gs.error('REST call failed: ' + ex.getMessage());
}

Want the complete reference?

This article is part of the NowSpectrum knowledge library. Browse all products for cheat sheets, interview prep, and deep-dive reference guides.

Browse All Products →