{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":1107,"path":"api-reference/server-api-reference/c_ScriptableServiceResponseBuilder.md","title":"RESTAPIResponse - Scoped, Global","bundle":null,"doc_type":null,"release":"australia","body":"\n# RESTAPIResponse - Scoped, Global\n\nThe RESTAPIResponse API provides methods that allow you to build a RESTful response to a scripted REST API request.\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## RESTAPIResponse - getStreamWriter\\(\\)\n\nReturns the ResponseStreamWriter for this response, allowing you to write directly to the response stream.\n\nSet the content type and status code using the setHeaders and setStatus functions prior to calling the getStreamWriter function.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n|Type|Description|\n|----|-----------|\n|RESTAPIResponseStream - Scoped, Global|The ResponseStreamWriter for this response. You can use this object to write directly to the response stream.|\n\n```\nresponse.setContentType('application/json');\nresponse.setStatus(200);\nvar writer = response.getStreamWriter();\n```\n\n## RESTAPIResponse - setBody\\(Object body\\)\n\nSets the body content to send in the web service response.\n\n<table id=\"table_yzt_cmr_qr\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nbody\n\n</td><td>\n\nObject\n\n</td><td>\n\nThe response body, as a JavaScript object.\n\n The body content is automatically serialized to JSON or XML depending on the value of the **Accept** header passed in the request.\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|void| |\n\n```\nvar body = {};\nbody.name = \"incident\";\nbody.number = \"1234\";\nbody.caller = {\"id\": \"user1\"};\nresponse.setBody(body);\n\n```\n\n```\nvar bodyArray = [];\nvar body = {};\nbody.name = \"incident\";\nbody.number = \"1234\";\nbody.caller = {\"id\":\"user1\"};\nbodyArray.push(body);\nresponse.setBody(bodyArray);\n```\n\n## RESTAPIResponse - setContentType\\(String contentType\\)\n\nAssigns a value to the Content-Type header in the web service response.\n\nYou 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.\n\nSetting 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.\n\nSee the [W3 Content-Type header documentation](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17) for more information about this header.\n\n|Name|Type|Description|\n|----|----|-----------|\n|contentType|String|The content type of the response body, such as application/json.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n```\nresponse.setContentType('application/json');\n```\n\n## RESTAPIResponse - setError\\(Object error\\)\n\nConfigures the REST response to return an error.\n\n<table id=\"table_apv_hb3_zs\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nerror\n\n</td><td>\n\nObject\n\n</td><td>\n\nError object. For more information about the types of error objects that can be used, see [Scripted REST API example - script samples](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/rest-api-explorer/r_ScriptedRESTServiceScriptExamples.md).\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|void| |\n\nThe following example shows how to return an error from within a scripted REST resource.\n\n```\nvar queryParams = request.queryParams;\nvar userId = String(queryParams.user_id || '');\nvar fileId = String(queryParams.file_id || '');\nif (!userId || !fileId){\n  response.setError(new sn_ws_err.BadRequestError('Missing required parameters.'));\n  return;\n}\n```\n\n## RESTAPIResponse - setHeader\\(String header, String value\\)\n\nAssign a value to a REST service response header.\n\n|Name|Type|Description|\n|----|----|-----------|\n|header|String|The header you want to set.|\n|value|String|The value to assign the specified header.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n```\nresponse.setHeader(\"Location\",\"<URI>\");\n```\n\n## RESTAPIResponse - setHeaders\\(Object headers\\)\n\nSets the headers for the web service response.\n\n|Name|Type|Description|\n|----|----|-----------|\n|headers|Object|A JavaScript object listing each header and the value to assign that header.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n```\nvar headers = {};\nheaders.X-Total-Count=100;\nheaders.Location='https://instance.service-now.com/<endpoint_to_resource>';\nresponse.setHeaders(headers);\n```\n\n## RESTAPIResponse - setLocation\\(String location\\)\n\nAssigns a value to the Location header in the web service response.\n\nSee the [W3 Location header documentation](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30) for more information about this header.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None|String|An absolute URI to redirect the response recipient to.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n## RESTAPIResponse - setStatus\\(Number status\\)\n\nSets the status code number for the web service response.\n\n|Name|Type|Description|\n|----|----|-----------|\n|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.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n```\nresponse.setStatus(200);\n```\n\n"}}