-
-
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
Koa always overwrite Content-Type
for JSON responses
#1120
Comments
Yes, setting body is basically a switch statement, where the default case is to assume value is json, which removes content-type and sets it to Lines 179 to 181 in 6baa411
I'm not sure about intent but this might be a feature. |
seems like a bug where it overwrites the content type when the content type is set and has |
Check if body has value of type object, and if the header contains |
Is there a merged fix for this? I'm making an API JSON API compliant but having to manually declare |
what's issue that keep the PR not merged? |
For reference, the PR in question is #1131 |
I'm just gonna note this here, for others to use, as I had a hard time finding a solution. A temporary "fix" to this, is to set a stringified JSON body, instead of a json object. Then the header will not be overridden, as we'll hit this condition where the ctx.body = JSON.stringify(data) |
There may be more nuance required than what is above, but Koa shouldn't overwrite a valid media type that has a structured syntax suffix like +json. A bit on why. Use of a more descriptive media types provides a parade of benefits. It can enable a far more expressive web, reduce guesswork in message processing, and provide for more graceful evolution of applications that handle structured data. Koa is amazing for being so flexible. It should make this possible, too. For reference, |
hello, I just want to confirm a workaround for the issue that @brunoabreu already wrote in his first post:
I tested it with version |
When running this code I aways get
Content-Type: application/json; charset=utf-8
.I can make it work if I write the header after setting the body. It only affects
json
responses. I'm not sure if that's a bug, but I couldn't find any information about that behavior in documentation.The text was updated successfully, but these errors were encountered: