-
-
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
Empty response if result is null #998
Comments
Nobody experiencing this?! :| |
|
@fengmk2 that's not good: what about an API endpoint returning "null" as data? |
Just my opinion but personally I most people expect an object or array with json these days, apis returning primitives is a little weird, even if it's technically fine. At very least something like |
@tj I agree, but in some cases it may be needed and returning a 204 could be unexpected. |
I don't have a super strong opinion there, I guess maybe ideally the client requesting stuff treats 204 as null but it's definitely easier to assume it's always JSON, though that's sort of the same weirdness as doing |
it makes way more sense to me to return is there absolutely no way you can change the FE code? this would be a breaking change |
@jonathanong if this would cause a breaking change then let's just leave it like this but I'm not sure pretending "always enveloped results" is right. My 2 cent: could koa check if request header is |
what do you think about it @tj? Could koa check if request header is |
Sounds reasonable, my main fear there is that it gets (even more) magical when it starts doing multiple things. I kind of agree that array/objects are the way to go, even if technically less correct |
@tj @jonathanong please let me know on this: if you think this is never going to be implemented I'll just close the issue. |
@tj @jonathanong time to close this? Did you take time to think if it's worthful or not? |
that sounds like a good behavior to have. if a content type is set, then the developer intends to set a body. my only concern is whether this would be a breaking change, which it probably will be for a few people |
Below is my case:
if ctx.body == null and ctx.status is 2XX or not set, we can set ctx.status to 204, otherwise it's better do nothing. |
After more than 2 years here I am again xD |
I'm willing to provide a PR. I saw this in the 3.0 roadmap initial issue #1114 as a breaking change. |
OK , If middleware:
( async (ctx) => {
ctx.body = null;
ctx.body = 'have result'
return ctx;
}) it still return 204 , anyone have this issue ? |
@samtsai15 I handle it something like this: const statuses = require('statuses');
// ...
app.use(async (ctx, next) => {
await next();
if (ctx.fresh) {
ctx.status = 304;
ctx.body = null;
} else if (ctx.length && statuses.empty[ctx.status]) {
ctx.status = 200;
}
}); |
@tj are you willing to solve this issue? We have some microservices in GraphQL and null is a perfect return value when it fits the contract. Im now wrapping Koa results back myself, so not a real dealbreaker but it's weird to see when explicitly returning null it magically disappears, and even worse gets another statusCode because of it. Also contractually now it doesn't fit, normally with service i do <Foo | null> and the contract is then consumed in the other mesh/gateway which now also requires manual work :(. I really liked the performance setup many years ago, but this really surprises me. |
@maapteh after 4 years I gave up and came up with a https://www.npmjs.com/package/koa-better-json |
Why the following returns an empty response?
null
is a valid json value.Reponse is empty:
The text was updated successfully, but these errors were encountered: