Hello community. I have a real problem here, can't...
# ktor
f
Hello community. I have a real problem here, can't seem to find info since all I can find out about this concerns the client side and not the server side of ktor. I have upgrade ktor from 1.6 to 2.2. This resulted in every json request response from my server having the
Content-Type: application/json
header instead of the
Content-Type: application/json; charset=UTF-8
which it had before. This causes my flutter app to display the wrong characters since it decodes everything all wrong. How do I fix this? Why is this change not documented? My entire API is broken.
Seems like its this but it is the other way around for me... Also, this concerns the client https://kotlinlang.slack.com/archives/C0A974TJ9/p1644027042441219
s
application/json; charset=UTF-8
isn’t a valid content type header. See also https://youtrack.jetbrains.com/issue/KTOR-3799
If the client expects a charset for JSON content, that’s a bug in the client.
f
Okay, thats good to know. So ktor returned invalid content type for several years before? I'll guess I have to check on flutter side then?
s
Yes 👍
For more detail: RFC 8259 specifies that JSON is always UTF-8. Previously (in RFC 7159) UTF-16 and UTF-32 were also allowed, but clients were expected to auto-detect it, and the charset was never included in the mime type.
f
found this
seems pretty anoyying to have
Copy code
convert.jsonDecode(utf8.decode(**))
on every single thing instead of
Copy code
jsonDecode(**)
Like, really?
s
Sounds like the problem is that
convert.jsonDecode
takes a String, which has already been decoded (using, potentially, the wrong encoding). Ideally there would be a method that takes the bytes and decodes them directly to JSON, without needing to go via a String.
f
alright, seems like its on the flutter side at least. I cannot keep track of all of these standards and stuff man, thank you so much for the info! Interesting change in ktor 1 to 2 though. But its a case of there it was wrong all the time and the reason it doesnt work for me is because my app is badly decoding right? hehe
👍 1
great...
its just silly me using a bad http client in dart
s
I think they have misinterpreted RFC-2616 😄 it says
When no explicit charset parameter is provided by the sender, media subtypes of the “text” type are defined to have a default charset value of “ISO-8859-1"
which is not only obsolete but also specifically doesn’t apply to application/json. That said, it’s a reasonable default if the user specifically wants to get the body as a String.
Are you able to pass
bodyBytes
directly to a JSON decoder of some kind? That might avoid the problem.
f
yep,
jsonDecode(utf8.decode(response.bodyBytes))
solves it for me
very beaiful and clean line for something you do everywhere 😄
I mean. I'm using a library that is based of this https://pub.dev/packages/http, an official dart package. That does this.
Interesting.
lol