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

时间:2021-03-01 04:47:57

标签: flutter http dart get header

我正在请求以下 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}.');
}

1 个答案:

答案 0 :(得分:1)

更改您的 tokenData 如下:

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

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