我目前正在使用 dart 进行 Web 开发。 使用模拟客户端实现服务。 但是,会发生以下错误。 下面的实现代码是一个内存中的web api服务,继承了mockClient。 调用client.send()并返回结果的代码。
test_value是json.encode(data)的结果。
var test_value = '{"id": 1, "type": "Appetizer", "name": "한글"}';
return Response (test_value, 200, headers: {'content-type': 'application / json'});
错误
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
如果在上面的实现代码的name中加上英文字符串,是不会报错的。 为什么我在插入非英文字符时会出错?
如果你知道请告诉我!
在韩国独自奋斗的dart程序员
最佳答案
Response
除非另有指定,否则类对正文使用 Latin-1 编码。
这在构造函数本身上没有明确记录,但关于 body
getter 的文档确实暗示了这一点。
尝试在 header 中设置字符集/编码,例如:
return Response(test_value, 200, headers: {
HttpHeaders.contentTypeHeader: 'application/json; charset=utf-8'
});
关于dart json.encode(data) 不能接受其他语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52990816/