RESTAPIResponse - Scoped, Global

A RESTAPIResponse object allows you to build a RESTful response to a scripted REST API request.

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()

Get 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.
response.setContentType('application/json');
response.setStatus(200);
var writer = response.getStreamWriter();

RESTAPIResponse - setBody(Object body)

Set 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

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

RESTAPIResponse - setContentType(String contentType)

Assign 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

responseBuilder.setContentType('application/json');

RESTAPIResponse - setError(Object error)

Configure the response to return an error.

Table 7. Parameters
Name Type Description
error Object An error object.
Table 8. Returns
Type Description
void

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
responseBuilder.setHeader("Location","<URI>");

RESTAPIResponse - setHeaders(Object headers)

Set 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

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

RESTAPIResponse - setLocation(String location)

Assign 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)

Set 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

response.setStatus(200);