Http: the response body become garbage if include Chinese chars in the response body

Created on 17 Feb 2019  路  2Comments  路  Source: dart-lang/http

I use Flutter 1.0 latest http package, the code get info from my REST API, it get some data from DB.
when some field contain chinese chars, the response body become garbage,
but if I use Postman to test the REST API, the data in response body is correct.

    var response = await http.post(
      constAWSAPIGateway_getProductByUser[CONST_API_STAGE],
      headers: {
        'Content-Type' : 'application/json; charset=utf-8',
      },
      body: json.encode(postBody),
      encoding: Utf8Codec() //this line doesn't make difference
    );

in postman response is correct:
{\"pid\":11,\"name\":\"瀹跺叿鍧犲瓙1\"}

in Flutter and your http package:
{"pid":11,"name":"氓庐露氓聟路氓聺 氓颅聬1"}

I try play around with the UTF8 setting but doesn't help.
so would you please look into this issue. or suggest kind of woraround?

thanks in advance.

Most helpful comment

after some search, found the reason as application/json default is utf-8 but missing the charset=utf-8 confused http package which treat as latin1.

my workaround is below:
//var responseJson = jsonDecode(response.body);
=>
var responseJson = json.decode(utf8.decode(response.bodyBytes));

then Chinese chars can display correctly. hope this help.

http might need add some handling for json.

All 2 comments

after some search, found the reason as application/json default is utf-8 but missing the charset=utf-8 confused http package which treat as latin1.

my workaround is below:
//var responseJson = jsonDecode(response.body);
=>
var responseJson = json.decode(utf8.decode(response.bodyBytes));

then Chinese chars can display correctly. hope this help.

http might need add some handling for json.

close this now as a simple workaround exists

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gdomingues picture gdomingues  路  5Comments

Jonas-Sander picture Jonas-Sander  路  6Comments

plake876 picture plake876  路  3Comments

drishit96 picture drishit96  路  3Comments

matanlurey picture matanlurey  路  6Comments