RESTResponseV2 - Scoped, Global

The RESTResponseV2 API allows you to use the data returned by an outbound REST message in JavaScript code.

A RESTResponseV2 object is returned by the RESTMessageV2 functions execute() and executeAsync().

You can use this API in scoped applications, or within the global scope.

RESTResponseV2 - getBody()

Get the content of the REST response body.

Use this function when you want to get the request body as text content. Do not use this method when saving the response as a binary attachment. If a RESTMessageV2 object called the saveResponseBodyAsAttachment(...) function, using getBody() on the associated RESTResponseV2 object will cause an error. When saving the response as an attachment, if the outbound REST message fails, call getErrorMessage() on the response to retrieve the body content.

Table 1. Parameters
Name Type Description
None
Table 2. Returns
Type Description
String The REST response body.
var sm = new sn_ws.RESTMessageV2("Yahoo Finance","get"); //Might throw exception if message doesn't exist or not visible due to scope.
var response = sm.execute();
var responseBody = response.getBody();

RESTResponseV2 - getCookies()

Get all cookies included in the response.

Table 3. Parameters
Name Type Description
None
Table 4. Returns
Type Description
Object The list of cookies. Iterate through the list to perform operations on each cookie.

var cookies = response.getCookies();
var i;
for(i=0;i<cookies.size();i++) {
   gs.print(‘cookie: ‘ + cookies.get(i));
}
Output:
  • JSESSIONID=4135AA97A5D12DA22EF614AA2B0CAFD8.node20; Path=/; Secure; HttpOnly
  • SABASESSIONID=370152970.36895.0000; path=/

RESTResponseV2 - getErrorCode()

Get the numeric error code if there was an error during the REST transaction.

This error code is specific to the ServiceNow platform, it is not an HTTP error code. Provide this error code if you require assistance from ServiceNow Customer Support

Table 5. Parameters
Name Type Description
None
Table 6. Returns
Type Description
Number The numeric error code, such as 1 for socket timeout.
var sm = new sn_ws.RESTMessageV2("Yahoo Finance","get"); //Might throw exception if message doesn't exist or not visible due to scope.
var response = sm.execute();
var errorCode = response.getErrorCode();

RESTResponseV2 - getErrorMessage()

Get the error message if there was an error during the REST transaction.

Table 7. Parameters
Name Type Description
None
Table 8. Returns
Type Description
String The error message.
var sm = new sn_ws.RESTMessageV2("Yahoo Finance","get"); //Might throw exception if message doesn't exist or not visible due to scope.
var response = sm.execute();
var errorMsg = response.getErrorMessage();

RESTResponseV2 - getHeader(String name)

Get the value for a specified header.

Table 9. Parameters
Name Type Description
name String The name of the header that you want the value for, such as Set-Cookie.
Table 10. Returns
Type Description
String The value of the specified header.
var sm = new sn_ws.RESTMessageV2("Yahoo Finance","get"); //Might throw exception if message doesn't exist or not visible due to scope.
var response = sm.execute();
var headerVal = response.getHeader("Content-Type");

RESTResponseV2 - getHeaders()

Get all headers returned in the REST response and the associated values.

Table 11. Parameters
Name Type Description
None
Table 12. Returns
Type Description
Object An Object that maps the name of each header to the associated value.
var sm = new sn_ws.RESTMessageV2("Yahoo Finance","get"); //Might throw exception if message doesn't exist or not visible due to scope.
var response = sm.execute();
var headers = response.getHeaders();

RESTResponseV2 - getQueryString()

Get the fully-resolved query sent to the REST endpoint..

This query contains the endpoint URL as well as any values assigned to variables in the REST message. Use this method only with responses to direct requests. This method is not supported for requests sent asynchronously, or requests sent using a MID server.

Table 13. Parameters
Name Type Description
None
Table 14. Returns
Type Description
String The fully-resolved query.
var sm = new sn_ws.RESTMessageV2("Yahoo Finance","get"); //Might throw exception if message doesn't exist or not visible due to scope.
var response = sm.execute();
var queryString = response.getQueryString();

RESTResponseV2 - getResponseAttachmentSysid()

Get the sys_id value of the attachment created from the response body content.

If the RESTMessageV2 object associated with this response called the saveResponseBodyAsAttachment(...) function, use getResponseAttachmentSysid() to get the sys_id of the created attachment record. Use this function when you want to perform additional operations with the new attachment record.

Table 15. Parameters
Name Type Description
None
Table 16. Returns
Type Description
String The sys_id of the new attachment record.

RESTResponseV2 - getStatusCode()

Get the numeric HTTP status code returned by the REST provider.

Table 17. Parameters
Name Type Description
None
Table 18. Returns
Type Description
Number The numeric status code returned by the REST provider, such as 200 for a successful response.
var sm = new sn_ws.RESTMessageV2("Yahoo Finance","get"); //Might throw exception if message doesn't exist or not visible due to scope.
var response = sm.execute();
var statusCode = response.getStatusCode();

RESTResponseV2 - haveError()

Indicate if there was an error during the REST transaction.

Table 19. Parameters
Name Type Description
None
Table 20. Returns
Type Description
boolean Returns true if there was an error, false if there was no error.
var sm = new sn_ws.RESTMessageV2("Yahoo Finance","get"); //Might throw exception if message doesn't exist or not visible due to scope.
var response = sm.execute();
var error = response.haveError();

RESTResponseV2 - waitForResponse(Number timeoutSecs)

Set the amount of time the instance waits for a response from the web service provider.

This method overrides the property glide.rest.outbound.ecc_response.timeout for this REST response.

Table 21. Parameters
Name Type Description
timeoutSecs Number The amount of time, in seconds, to wait for this response.
Table 22. Returns
Type Description
void
var sm = new sn_ws.RESTMessageV2("Yahoo Finance","get"); //Might throw exception if message doesn't exist or not visible due to scope.
var response = sm.executeAsync();
response.waitForResponse(60);