RESTAPIResponse - Scoped, Global
-
- UpdatedJan 30, 2025
- 3 minutes to read
- Yokohama
- API reference
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.
RESTAPIResponse - getStreamWriter()
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.
Name | Type | Description |
---|---|---|
None |
Type | Description |
---|---|
RESTAPIResponseStream - Scoped, Global | The ResponseStreamWriter for this response. You can use this object to write directly to the response stream. |
Example
RESTAPIResponse - setBody(Object body)
Sets the body content to send in the web service response.
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. |
Type | Description |
---|---|
void |
Example
Example
RESTAPIResponse - setContentType(String contentType)
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 for more information about this header.
Name | Type | Description |
---|---|---|
contentType | String | The content type of the response body, such as application/json. |
Type | Description |
---|---|
void |
Example
RESTAPIResponse - setError(Object error)
Configures the REST response to return an error.
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. |
Type | Description |
---|---|
void |
Example
The following example shows how to return an error from within a scripted REST resource.
RESTAPIResponse - setHeader(String header, String value)
Assign a value to a REST service response header.
Name | Type | Description |
---|---|---|
header | String | The header you want to set. |
value | String | The value to assign the specified header. |
Type | Description |
---|---|
void |
Example
RESTAPIResponse - setHeaders(Object headers)
Sets the headers for the web service response.
Name | Type | Description |
---|---|---|
headers | Object | A JavaScript object listing each header and the value to assign that header. |
Type | Description |
---|---|
void |
Example
RESTAPIResponse - setLocation(String location)
Assigns a value to the Location header in the web service response.
See the W3 Location header documentation for more information about this header.
Name | Type | Description |
---|---|---|
None | String | An absolute URI to redirect the response recipient to. |
Type | Description |
---|---|
void |
RESTAPIResponse - setStatus(Number status)
Sets the status code number for the web service response.
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. |
Type | Description |
---|---|
void |
Example
On this page
- RESTAPIResponse - getStreamWriter()
- RESTAPIResponse - setBody(Object body)
- RESTAPIResponse - setContentType(String contentType)
- RESTAPIResponse - setError(Object error)
- RESTAPIResponse - setHeader(String header, String value)
- RESTAPIResponse - setHeaders(Object headers)
- RESTAPIResponse - setLocation(String location)
- RESTAPIResponse - setStatus(Number status)