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


---

# RESTAPIResponseStream - Scoped, Global

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

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

The RESTAPIResponseStream API provides methods that allow you to write directly to the scripted REST API response stream.

Use RESTAPIResponseStream methods to build web service APIs in the [Scripted REST API](https://servicenow-prod.fluidtopics.net/x1EOw4mDLxo1z7QG_tlNhQ "The scripted REST API feature allows application developers to build custom web service APIs.") feature.

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.

## RESTAPIResponseStream - writeStream(Object stream) {#ariaid-title2}

Write an input stream to the response stream.
You must set the content type and status code before calling the
writeStream() method or the response will fail. You cannot modify these
values after calling the writeStream() method.  
Note:  
It is the responsibility of the script author to obtain the stream from a third-party
service.
{#r_SSRSW-writeStream_O__table_c2x_wnb_sr__entry__3}

| Name | Type | Description |
|-|-|-|
| stream | Object | An attachment or a response stream from a third-party service. |
[Table 1. Parameters]

{#r_SSRSW-writeStream_O__table_c2x_wnb_sr} {#r_SSRSW-writeStream_O__table_d2x_wnb_sr__entry__2}

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

{#r_SSRSW-writeStream_O__table_d2x_wnb_sr}  
The following example is for scoped applications:

    (function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

        response.setContentType('application/json');
        response.setStatus(200);

        var gsa = new GlideSysAttachment();
        var attachmentStream = new gsa.getContentStream(<sys_id of attachment>); 
        var writer = response.getStreamWriter();
        writer.writeStream(attachmentStream);

    })(request, response);

The following example is for global applications:

    (function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

        response.setContentType('application/json');
        response.setStatus(200);

        var attachmentStream = new GlideSysAttachmentInputStream(<sys_id of attachment>);
        var writer = response.getStreamWriter();
        writer.writeStream(attachmentStream);

    })(request, response);

## RESTAPIResponseStream - writeString(String data) {#ariaid-title3}

Write string data to the response stream.
You must set the content type and status code before calling the
writeString() method or the response will fail. You cannot modify these
values after calling the writeString() method.
{#r_SSRSW-writeString_S__table_gck_rcr_qr__entry__3}

| Name | Type | Description |
|-|-|-|
| data | String | The string to add to the response data. |
[Table 3. Parameters]

{#r_SSRSW-writeString_S__table_gck_rcr_qr} {#r_SSRSW-writeString_S__table_hck_rcr_qr__entry__2}

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

{#r_SSRSW-writeString_S__table_hck_rcr_qr}  

    response.setContentType('application/json');
    response.setStatus(200);
    var writer = response.getStreamWriter();
    var body ={
      name:user1,
      id: 1234,
      roles: [
        {
          name: admin
        },
        {
          name: itil
        }
      ]
    }
    writer.writeString("{'name':'user','id':'1234'}");
    writer.writeString(JSON.stringify(body));


