-
Notifications
You must be signed in to change notification settings - Fork 363
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
Fix Default Encoding for application/json Content-Type in HTTP Responses #1422
base: master
Are you sure you want to change the base?
Conversation
can anyone reply to my pull request? |
Great but, could you try to pass some Chinese or Korean language tests through conformance tests? @snoopdoggy322. |
Ooo...could be good |
PR HealthChangelog Entry ❗
Changes to files need to be accounted for in their respective changelogs. This check can be disabled by tagging the PR with Coverage ✔️
This check for test coverage is informational (issues shown here will not fail the PR). API leaks ✔️The following packages contain symbols visible in the public API, but not exported by the library. Export these symbols or remove them from your publicly visible API.
License Headers ✔️
All source files should start with a license header. Unrelated files missing license headers
|
pkgs/http/lib/src/response.dart
Outdated
/// encoding name is unknown, [latin1] is used by default, as per | ||
/// [RFC 2616][]. | ||
/// encoding name is unknown, [utf8] is used by default, as per | ||
/// [RFC3629][]. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you site a section? Is this only for JSON data for for all payloads?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, RFC3629 requires interpreting application/json as UTF-8, everything else is described as incorrect interpretation of the specification
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent work, we are almost there!
Would you also:
- change the version in pubspec.yaml to version: 1.4.0-wip
- add a 1.4.0-wip section to CHANGELOG.md with a description of your change
final utf8Bytes = utf8.encode('{"foo":"Привет, мир!"}'); | ||
var response = http.Response.bytes(utf8Bytes, 200, | ||
headers: {'content-type': 'application/json'}); | ||
expect(response.body, equals('{"foo":"Привет, мир!"}')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, there should be one except
per test because you are testing a single scenario.
Could you add tests for these additional scenarios:
- test
.body
with a non-empty charset with content-type application/json data (e.g. with Russian and iso-8859-1) - test
.body
with an empty charset with a content-type of test/plain (e.g. with Russian to verify that the default of iso-8859-1 is used)
/// charset is unknown. | ||
/// If the `Content-Type` header specifies a charset, it will use that charset. | ||
/// If no charset is provided or the charset is unknown: | ||
/// - Defaults to [utf8] if the `Content-Type` is `application/json` (since JSON is defined to use UTF-8 by default). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrap to 80 columns
@@ -21,10 +21,10 @@ class Response extends BaseResponse { | |||
/// | |||
/// This is converted from [bodyBytes] using the `charset` parameter of the | |||
/// `Content-Type` header field, if available. If it's unavailable or if the | |||
/// encoding name is unknown, [latin1] is used by default, as per | |||
/// [RFC 2616][]. | |||
/// encoding name is unknown, [utf8] is used by default, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is only when content-type is UTF-8 right? So could you clarify that in your comments e.g.
If it's unavailable or if the encoding name is unknown:
- [utf8] is used when the content-type is
'application/json'
(see [RFC 3629]). - [latin1] is used in all other cases (see [RFC 2616][]).
This merge request addresses a long-standing issue (#175) where the http package defaults to latin1 encoding when no charset is specified in the Content-Type header. Per RFC 8259, the default encoding for application/json should be utf-8.
The fix ensures that responses with Content-Type: application/json and no explicit charset parameter are decoded using utf8 by default. This change improves compliance with the JSON standard and prevents decoding errors for valid JSON payloads encoded in utf-8.