{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":1106,"path":"api-reference/server-api-reference/c_ScriptableServiceRespStreamWriter.md","title":"RESTAPIResponseStream - Scoped, Global","bundle":null,"doc_type":null,"release":"australia","body":"\n# RESTAPIResponseStream - Scoped, Global\n\nThe RESTAPIResponseStream API provides methods that allow you to write directly to the scripted REST API response stream.\n\nUse RESTAPIResponseStream methods to build web service APIs in the [Scripted REST API](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/rest-api-explorer/c_CustomWebServices.md) feature.\n\nThis API runs in the `sn_ws` namespace.\n\n**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.\n\n**Parent Topic:**[Server API reference](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/api-server.md)\n\n## RESTAPIResponseStream - writeStream\\(Object stream\\)\n\nWrite an input stream to the response stream.\n\nYou 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.\n\n**Note:**\n\nIt is the responsibility of the script author to obtain the stream from a third-party service.\n\n|Name|Type|Description|\n|----|----|-----------|\n|stream|Object|An attachment or a response stream from a third-party service.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\nThe following example is for scoped applications:\n\n```\n(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {\n\n    response.setContentType('application/json');\n    response.setStatus(200);\n\n    var gsa = new GlideSysAttachment();\n    var attachmentStream = new gsa.getContentStream(<sys_id of attachment>); \n    var writer = response.getStreamWriter();\n    writer.writeStream(attachmentStream);\n\n})(request, response);\n```\n\nThe following example is for global applications:\n\n```\n(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {\n\n    response.setContentType('application/json');\n    response.setStatus(200);\n\n    var attachmentStream = new GlideSysAttachmentInputStream(<sys_id of attachment>);\n    var writer = response.getStreamWriter();\n    writer.writeStream(attachmentStream);\n\n})(request, response);\n```\n\n## RESTAPIResponseStream - writeString\\(String data\\)\n\nWrite string data to the response stream.\n\nYou 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.\n\n|Name|Type|Description|\n|----|----|-----------|\n|data|String|The string to add to the response data.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n```\nresponse.setContentType('application/json');\nresponse.setStatus(200);\nvar writer = response.getStreamWriter();\nvar body ={\n  name:user1,\n  id: 1234,\n  roles: [\n    {\n      name: admin\n    },\n    {\n      name: itil\n    }\n  ]\n}\nwriter.writeString(\"{'name':'user','id':'1234'}\");\nwriter.writeString(JSON.stringify(body));\n```\n\n"}}