The RESTAPIResponse API provides methods that allow you to build a RESTful response to a scripted REST API request.

This API runs in the sn_ws namespace.

Note: You cannot instantiate objects of this type. Objects of this type are created automatically and are accessible only in scripted REST API resource scripts.

RESTAPIResponse - getStreamWriter()

Returns the ResponseStreamWriter for this response, allowing you to write directly to the response stream.

Set the content type and status code using the setHeaders and setStatus functions prior to calling the getStreamWriter function.

Table 1. Parameters
Name Type Description
None
Table 2. Returns
Type Description
RESTAPIResponseStream - Scoped, Global The ResponseStreamWriter for this response. You can use this object to write directly to the response stream.

Example

response.setContentType('application/json');
response.setStatus(200);
var writer = response.getStreamWriter();

RESTAPIResponse - setBody(Object body)

Sets the body content to send in the web service response.

Table 3. Parameters
Name Type Description
body Object

The response body, as a JavaScript object.

The body content is automatically serialized to JSON or XML depending on the value of the Accept header passed in the request.

Table 4. Returns
Type Description
void

Example

var body = {};
body.name = "incident";
body.number = "1234";
body.caller = {"id": "user1"};
response.setBody(body);

Example

var bodyArray = [];
var body = {};
body.name = "incident";
body.number = "1234";
body.caller = {"id":"user1"};
bodyArray.push(body);
response.setBody(bodyArray);

RESTAPIResponse - setContentType(String contentType)

Assigns a value to the Content-Type header in the web service response.

You must set a response content type before writing the response. The content type is set automatically for string responses, based on the request Accept header value.

Setting an invalid content type causes the response to default to JSON. Failing to set a content type results in a status code 500 error when sending a binary response.

See the W3 Content-Type header documentation for more information about this header.

Table 5. Parameters
Name Type Description
contentType String The content type of the response body, such as application/json.
Table 6. Returns
Type Description
void

Example

response.setContentType('application/json');

RESTAPIResponse - setError(Object error)

Configures the REST response to return an error.

Table 7. Parameters
Name Type Description
error Object Error object.

For more information about the types of error objects that can be used, see Scripted REST API example - script samples.

Table 8. Returns
Type Description
void

Example

The following example shows how to return an error from within a scripted REST resource.

var queryParams = request.queryParams;
var userId = String(queryParams.user_id || '');
var fileId = String(queryParams.file_id || '');
if (!userId || !fileId){
  response.setError(new sn_ws_err.BadRequestError('Missing required parameters.'));
  return;
}

RESTAPIResponse - setHeader(String header, String value)

Assign a value to a REST service response header.

Table 9. Parameters
Name Type Description
header String The header you want to set.
value String The value to assign the specified header.
Table 10. Returns
Type Description
void

Example

response.setHeader("Location","<URI>");

RESTAPIResponse - setHeaders(Object headers)

Sets the headers for the web service response.

Table 11. Parameters
Name Type Description
headers Object A JavaScript object listing each header and the value to assign that header.
Table 12. Returns
Type Description
void

Example

var headers = {};
headers.X-Total-Count=100;
headers.Location='https://instance.service-now.com/<endpoint_to_resource>';
response.setHeaders(headers);

RESTAPIResponse - setLocation(String location)

Assigns a value to the Location header in the web service response.

See the W3 Location header documentation for more information about this header.

Table 13. Parameters
Name Type Description
None String An absolute URI to redirect the response recipient to.
Table 14. Returns
Type Description
void

RESTAPIResponse - setStatus(Number status)

Sets the status code number for the web service response.

Table 15. Parameters
Name Type Description
status Number The status code to send in the response, such as 200 to indicate success. Passing a non-numerical value, such as a string, causes the status code to default to 0.
Table 16. Returns
Type Description
void

Example

response.setStatus(200);