Skip to content

Commit 38cb591

Browse files
AlbertAZ1992miwnwski
authored andcommitted
docs: update specific for auto response status
1 parent 2224cd9 commit 38cb591

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

docs/api/response.md

+11-2
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,18 @@ ctx.response.status = 204;
130130
- `Buffer` written
131131
- `Stream` piped
132132
- `Object` || `Array` json-stringified
133-
- `null` no content response
133+
- `null` || `undefined` no content response
134+
135+
If `response.status` has not been set, Koa will automatically set the status to `200` or `204` depending on `response.body`. Specifically, if `response.body` has not been set or has been set as `null` or `undefined`, Koa will automatically set `response.status` to `204`. If you really want to send no content response with other status, you should override the `204` status as the following way:
136+
137+
```js
138+
// This must be always set first before status, since null | undefined
139+
// body automatically sets the status to 204
140+
ctx.body = null;
141+
// Now we override the 204 status with the desired one
142+
ctx.status = 200;
143+
```
134144

135-
If `response.status` has not been set, Koa will automatically set the status to `200` or `204`.
136145

137146
Koa doesn't guard against everything that could be put as a response body -- a function doesn't serialise meaningfully, returning a boolean may make sense based on your application, and while an error works, it may not work as intended as some properties of an error are not enumerable. We recommend adding middleware in your app that asserts body types per app. A sample middleware might be:
138147

0 commit comments

Comments
 (0)