GET 请求在 Dart 中为 http 包 Flutter 显示“406 - Not Acceptable”

问题描述

我正在请求以下 Get 请求,但收到“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}.');
}

解决方法

更改您的 tokenData 如下:

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

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