Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dart json.encode(data) can not accept other language

I am currently doing web development with dart. Implemented service with mockclient. However, the following error occurs. The implementation code below is an in memory web api service that inherits mockClient. The code that calls client.send () and returns the result.

test_value is the result of json.encode (data).

var test_value = '{"id": 1, "type": "Appetizer", "name": "한글"}';

     return Response (test_value, 200, headers: {'content-type': 'application / json'});

ERROR

Invalid argument(s): String contains invalid characters.
dart:convert                                           Latin1Codec.encode
package:http/src/response.dart 36:49                   new Response
package:basil/common/mock_rest/mock_recipe.dart 40:12  MockRecipe._handler

If you put an English string in the name of the above implementation code, there is no error. Why do I get an error when I insert a character other than English?

Please let me know if you know!

A dart programmer struggling alone in Korea

like image 927
superpeace Avatar asked Oct 25 '18 13:10

superpeace


1 Answers

The Response class uses Latin-1 encoding for the body unless something else is specified. This is not documented clearly on the constructor itself, but the documentation on the body getter does suggest this.

Try setting the charset/encoding in the header, e.g., as:

return Response(test_value, 200, headers: {
    HttpHeaders.contentTypeHeader: 'application/json; charset=utf-8'
});
like image 86
lrn Avatar answered Oct 09 '22 01:10

lrn