-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Allow non 204 status for null
JSON responses
#1421
Conversation
Introduces a new application option `emptyBodyAs204` that must be set explictly to `false` when creating the Koa app to prevent Koa from treating `null` body as 204. Set it via ```js new Koa({ response: { emptyBodyAs204: false } }); ``` Closes #998
Codecov Report
@@ Coverage Diff @@
## master #1421 +/- ##
==========================================
+ Coverage 99.37% 99.38% +<.01%
==========================================
Files 4 4
Lines 479 485 +6
Branches 128 129 +1
==========================================
+ Hits 476 482 +6
Partials 3 3
Continue to review full report at Codecov.
|
why not set the status after setting |
It's reasonable to use 204 if the body is |
@jonathanong @yorkie I'm not sure 204 is correct: there's a body because there's a payload, a json-serializable payload. I'm not sure whether you are understanding what I mean 🤔 |
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null
I think the primitive @damianobarbati as I mentioned before, how about adding an option to disable the automatic status reset globally, which will make the status will not be set to 204 when you set the body to null, and it might be more useful usually. |
That's what this PR provides, isn't it? |
This PR is just adding an option for a specific case |
I don't think add this option is a good idea, it is easy to implement a middleware to handle this. (ctx, next) => {
await next();
if (ctx.body === null) {
ctx.body = 'null';
}
} |
Great idea! I think that should work for you @damianobarbati? |
@pke @dead-horse thanks but I did already try before opening the issue a very long time ago and I had no way to overcome the 204: I suspect Koa is doing something when body is null preventing me from setting any body. In the following snippet am I doing something wrong? Is it supposed to work? import koa from 'koa';
const app = new koa();
app.use(async (ctx, next) => {
await next();
if (ctx.body === null) {
ctx.set('Content-Type', 'application/json; charset=utf-8');
ctx.body = 'null';
}
});
app.use(async ctx => ctx.body = null);
app.listen(80) |
@yorkie yes I think an option to prevent the aforementioned (default) behaviour of Koa would work. The 204 on json payload of It's ambiguous when it comes to API request and response:
But the producer must return a message (json string) expressing the content is null ( Koa has a right default behaviour covering most of cases, still I think an opt-out should be given to developers. But I'm just discussing/brainstorming guys! It's not my intention to start a flame 😅
|
Introduces a new application option
emptyBodyAs204
that must be set explictly tofalse
when creating the Koa app to prevent Koa from treatingnull
body as 204.Set it via
Closes #998
I'll provide documentation updates when we decided on a final name.