Skip to content

Commit a2836b4

Browse files
committed
Document Response object
1 parent d992670 commit a2836b4

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

docs/technical-details/reference/resource.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,18 @@ for (let key in plainObject) {
597597
}
598598
```
599599
600+
## Response Object
601+
The resource methods can return an object that will be serialized and returned as the response to the client. However, these methods can also return a `Response` style object with `status`, `headers`, and optionally `body` or `data` properties. This allows you to have more control over the response, including setting custom headers and status codes. For example, you could return a redirect response like:
602+
603+
```javascript
604+
return { status: 302, headers: { Location: '/new-location' } };
605+
```
606+
If you include a `body` property, this must be a string or buffer that will be returned as the response body. If you include a `data` property, this must be an object that will be serialized as the response body (using the standard content negotiation). For example, we could return an object with a custom header:
607+
608+
```javascript
609+
return { status: 200, headers: { 'X-Custom-Header': 'custom value' }, data: { message: 'Hello, World!' } };
610+
```
611+
600612
### Throwing Errors
601613
602614
You may throw errors (and leave them uncaught) from the response methods and these should be caught and handled by protocol the handler. For REST requests/responses, this will result in an error response. By default the status code will be 500. You can assign a property of `statusCode` to errors to indicate the HTTP status code that should be returned. For example:

0 commit comments

Comments
 (0)