Invalid Arabic characters With Utf-8 charset Retrived with http.get Flutter

The web server's Content-Type header is Content-Type: text/html. Note that isn't including a charset suffix. It should be saying Content-Type: text/html; charset=utf-8. The package:http client looks for this charset when asked to decode to characters. If it's missing it defaults to LATIN1 (not utf-8).

As you've seen, setting the headers on the Request doesn't help, as it's the Response that does the decoding. Luckily, there's a simple fix. Just decode the bytes to String yourself like this.

Future<String> loadFarsi() async {
  final response =
      await http.get("http://mobagym.com/media/mobagym-app-info/farsi.html");
  String body = utf8.decode(response.bodyBytes);
  print(body);
  return body;
}

Tags:

Dart

Flutter