首页 > 解决方案 > GET 请求在 Dart 中为 http 包 Flutter 显示“406 - Not Acceptable”

问题描述

我正在请求以下获取请求,但收到“406 - 不可接受”响应。在这里我附上了请求代码:

final Map<String, String> tokenData = {"Authorization": token};

var response = await http.get(url, headers: tokenData);
if (response.statusCode == 200) {
  var jsonResponse = convert.jsonDecode(response.body);
  var itemCount = jsonResponse['totalItems'];
  print('Number of books about http: $itemCount.');
} else {
  print('Request failed with status: ${response.statusCode}.');
  print('Request failed with reason: ${response.reasonPhrase}.');
}

标签: flutterhttpdartgetheader

解决方案


更改您的tokenData 如下:

 final Map<String, String> tokenData = {
    'Authorization': token
    'Content-type': 'application/json',
    'Accept': 'application/json',
  };

如果您将 JSON 作为响应,它将起作用。


推荐阅读