---
sourceDocument: Xanadu API Reference
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/xanadu/api-reference

 Release :

    - xanadu

ft:locale :

    - en-US

ft:publication_title :

    - Xanadu API Reference

ft:clusterId :

    - crapiref

bundleId :

    - crapiref

workflow :

    - Creator


---

# RESTAPIResponse - Scoped, Global

# RESTAPIResponse - Scoped, Global {#ariaid-title1}

* Release version: Xanadu
* 
* Updated August 1, 2024
* 
* ![](https://www.servicenow.com/docs/portal-asset/ico-clock) 2 minutes to read

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() {#ariaid-title2}

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.
{#r_SSRB-getStreamWriter__table_wnn_glb_sr__entry__3}

| Name | Type | Description |
|-|-|-|
| None |   |   |
[Table 1. Parameters]

{#r_SSRB-getStreamWriter__table_wnn_glb_sr} {#r_SSRB-getStreamWriter__table_xnn_glb_sr__entry__2}

| Type | Description |
|-|-|
| RESTAPIResponseStream - Scoped, Global | The ResponseStreamWriter for this response. You can use this object to write directly to the response stream. |
[Table 2. Returns]

{#r_SSRB-getStreamWriter__table_xnn_glb_sr}  

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

## RESTAPIResponse - setBody(Object body) {#ariaid-title3}

Sets the body content to send in the web service response.
{#r_SSRB-setBody_Object__table_yzt_cmr_qr__entry__3}

| 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 3. Parameters]

{#r_SSRB-setBody_Object__table_yzt_cmr_qr} {#r_SSRB-setBody_Object__table_zzt_cmr_qr__entry__2}

| Type | Description |
|-|-|
| void |   |
[Table 4. Returns]

{#r_SSRB-setBody_Object__table_zzt_cmr_qr}  

    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) {#ariaid-title4}

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](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17) for more
information about this header.
{#r_SSRB-setContentType_String__table_dcn_3lr_qr__entry__3}

| Name | Type | Description |
|-|-|-|
| contentType | String | The content type of the response body, such as application/json. |
[Table 5. Parameters]

{#r_SSRB-setContentType_String__table_dcn_3lr_qr} {#r_SSRB-setContentType_String__table_ecn_3lr_qr__entry__2}

| Type | Description |
|-|-|
| void |   |
[Table 6. Returns]

{#r_SSRB-setContentType_String__table_ecn_3lr_qr}  

    response.setContentType('application/json');

## RESTAPIResponse - setError(Object error) {#ariaid-title5}

Configures the REST response to return an error.
{#r_SSRB-setError_O__table_apv_hb3_zs__entry__3}

| 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](https://servicenow-prod.fluidtopics.net/ilmJvrsdRorAUQfnVzDj~A "These examples demonstrate how to create various resource scripts for a scripted REST API."). |
[Table 7. Parameters]

{#r_SSRB-setError_O__table_apv_hb3_zs} {#r_SSRB-setError_O__table_bpv_hb3_zs__entry__2}

| Type | Description |
|-|-|
| void |   |
[Table 8. Returns]

{#r_SSRB-setError_O__table_bpv_hb3_zs}  
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) {#ariaid-title6}

Assign a value to a REST service response header.
{#r_SSRB-setHeader_String_String__table_und_rt4_gs__entry__3}

| Name | Type | Description |
|-|-|-|
| header | String | The header you want to set. |
| value | String | The value to assign the specified header. |
[Table 9. Parameters]

{#r_SSRB-setHeader_String_String__table_und_rt4_gs} {#r_SSRB-setHeader_String_String__table_vnd_rt4_gs__entry__2}

| Type | Description |
|-|-|
| void |   |
[Table 10. Returns]

{#r_SSRB-setHeader_String_String__table_vnd_rt4_gs}  

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

## RESTAPIResponse - setHeaders(Object headers) {#ariaid-title7}

Sets the headers for the web service response.
{#r_SSRB-setHeaders_Object__table_slb_j3r_qr__entry__3}

| Name | Type | Description |
|-|-|-|
| headers | Object | A JavaScript object listing each header and the value to assign that header. |
[Table 11. Parameters]

{#r_SSRB-setHeaders_Object__table_slb_j3r_qr} {#r_SSRB-setHeaders_Object__table_tlb_j3r_qr__entry__2}

| Type | Description |
|-|-|
| void |   |
[Table 12. Returns]

{#r_SSRB-setHeaders_Object__table_tlb_j3r_qr}  

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

## RESTAPIResponse - setLocation(String location) {#ariaid-title8}

Assigns a value to the Location header in the web service response.
See the [W3 Location header documentation](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30) for more
information about this header.
{#r_SSRB-setLocation_String__table_mzd_1jr_qr__entry__3}

| Name | Type | Description |
|-|-|-|
| None | String | An absolute URI to redirect the response recipient to. |
[Table 13. Parameters]

{#r_SSRB-setLocation_String__table_mzd_1jr_qr} {#r_SSRB-setLocation_String__table_nzd_1jr_qr__entry__2}

| Type | Description |
|-|-|
| void |   |
[Table 14. Returns]

{#r_SSRB-setLocation_String__table_nzd_1jr_qr}

## RESTAPIResponse - setStatus(Number status) {#ariaid-title9}

Sets the status code number for the web service response.
{#r_SSRB-setStatus_Number__table_tj5_khr_qr__entry__3}

| 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 15. Parameters]

{#r_SSRB-setStatus_Number__table_tj5_khr_qr} {#r_SSRB-setStatus_Number__table_uj5_khr_qr__entry__2}

| Type | Description |
|-|-|
| void |   |
[Table 16. Returns]

{#r_SSRB-setStatus_Number__table_uj5_khr_qr}  

    response.setStatus(200);


