http icon indicating copy to clipboard operation
http copied to clipboard

Issue Decoding Special Characters (response.body)

Open lcsvcn opened this issue 3 years ago • 6 comments

Issue decoding special characters such as:

ã --> didn't test with any other special chars, but should be happening too. Such as ç, õ, á, à, ó, í, and so on...

Code:

final response = await http.get(
      serverUrl,
      ),
      headers: {
         "accept": "application/json;",
      },
    );
  
   print(response.body);  // prints { "title":"Não encontrado" } -- should be {"title":"Não encontrado"}

Expected:

{ "title": "Não encontrado" }

Reality:

{ "title":"Não encontrado" }

lcsvcn avatar Sep 09 '22 21:09 lcsvcn

Found a workaround:

Use:

json.decode(utf8.decode(response.bodyBytes))}

Instead of:

json.decode(response.body);

It works like a charm!

I still think that the response.body should be able to decode any UTF-8 characters, so the issue exists, maybe some of the http library developers can confirm.

Because in my understanding, even with request headers {"charset": "utf-8"} it doesn`t decode from utf-8 what is kind of strange behaviour.

lcsvcn avatar Sep 09 '22 21:09 lcsvcn

I haven't touched Flutter for a while, but I didn't realize this issue was not closed yet... It's been there for at least 3 years now. When will we be able to write jsonDecode(response.body) when the default for application/json is utf-8 even in the RFC? I'm wondering why it hasn't been fixed.

aoisensi avatar Sep 13 '22 21:09 aoisensi

I have a problem in this function, it was working 0.13.4 but now, it cannot work , image

utkudenis avatar Nov 18 '22 22:11 utkudenis

https://github.com/dart-lang/http/blob/master/pkgs/http/lib/src/utils.dart#LL28C67-L28C73 image image so retarded

medz avatar Mar 21 '23 13:03 medz

Found a workaround:

Use:

json.decode(utf8.decode(response.bodyBytes))}

Instead of:

json.decode(response.body);

It works like a charm!

I still think that the response.body should be able to decode any UTF-8 characters, so the issue exists, maybe some of the http library developers can confirm.

Because in my understanding, even with request headers {"charset": "utf-8"} it doesn`t decode from utf-8 what is kind of strange behaviour.

thanks, it solves my Turkish character (ş, ı,ç etc) problems

FlutterWiz avatar Apr 30 '23 16:04 FlutterWiz

the solution for me was this : https://pub.dev/packages/html_unescape

var unescape = HtmlUnescape();
      var text = unescape.convert(response.body);
      print(text);
      final data = jsonDecode(text);

abdoutech93 avatar Aug 05 '23 15:08 abdoutech93