Skip to content
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

Improper Text Encoding (UTF-8) #234

Open
drexhacker opened this issue Jul 6, 2023 · 5 comments
Open

Improper Text Encoding (UTF-8) #234

drexhacker opened this issue Jul 6, 2023 · 5 comments

Comments

@drexhacker
Copy link

Hello I have a REST Server returning JSON Responses using UTF-8 when I request the data through flutter_data I receive wrongly encoded text for example instead of getting agakuŋŋaanye go n’agayita as sent by the server, I get agakuÅÅaanye go nâagayita. I am using the defualt headers;

{
  "Content-Type": "application/json"
}

Really Need help here, everything works fine except this simple issue.

@drexhacker
Copy link
Author

@frank06 I think the issue revolves back to UTF-8 charset for example my application supports various languages meaning it contains various characters in the UTF-8 encoding but maybe somehow somewhere they are limited. I found an almost similar issue in hivedb isar/hive#928 which I think somehow it relates to this.

@drexhacker
Copy link
Author

Finally managed to solve the issue by modifying the sendRequest of the plugin locally on my Computer like below.

Original Version

...

    // response handling

    var contentType = 'application/json';

    try {
      if (response?.body.isNotEmpty ?? false) {
        contentType = response!.headers['content-type'] ?? contentType;
        final body = response.body;
        if (contentType.contains('json')) {
          responseBody = json.decode(body);
        } else {
          responseBody = response.body;
        }
      }
    } on FormatException catch (e, stack) {
      error = e;
      stackTrace = stack;
    }

...

Modified Version

...

    // response handling

    var contentType = 'application/json';

    try {
      if (response?.body.isNotEmpty ?? false) {
        contentType = response!.headers['content-type'] ?? contentType;
        if (contentType.contains('json')) {
          responseBody = json.decode(utf8.decode(response.bodyBytes));
        } else {
          responseBody = response.body;
        }
      }
    } on FormatException catch (e, stack) {
      error = e;
      stackTrace = stack;
    }

...

Hopefully it Helps Someone. But also take note that this for majorly UTF-8 Encoding, use with care.
@frank06 looking forward to your response on this, preferably a better and permanent solution.

@frank06
Copy link
Contributor

frank06 commented Aug 4, 2023

Glad you found a workaround! I am hesitant to hardcode utf8 decoding so I'd need to analyze the situation. In tests, and probably if supplied by a backend, the header 'content-type': 'application/json; charset=utf-8' seems to make the response utf8 decoded whereas without it, the http package defaults to Latin1

@alexrothm
Copy link

alexrothm commented Apr 16, 2024

Hey, I had the same issue. I should have read the workaround earlier... spent 2 hours trying to fix the encoding.

The JSON spec says that:

JSON text SHALL be encoded in Unicode. The default encoding is UTF-8. (RFC4627)

@frank06
Copy link
Contributor

frank06 commented Apr 24, 2024

Thinking of changing application/json on the fly to application/json; charset=utf-8

@frank06 frank06 reopened this Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants