扑http请求/响应的详细信息,包括标题?

时间:2020-07-01 22:52:29

标签: flutter dart

虽然http的请求详细信息可以在浏览器开发工具(针对Web应用程序)中轻松检查,但我尝试探索,在哪里可以找到针对flutter App中发送的请求的请求详细信息,但找不到它。

例如-我可以看到print(response)来自api的实际响应,但是我说的是完整的请求响应,包括headers

我正在将VScode IDE用于Flutter。

更新:

我想查看像response.header这样发送的标题。相同的原因就像我正在使用flutter cahe manager,而面临的问题就像我设置了-cache control max-age=1

因此,每当我访问页面时,颤动应该尝试获取相同的内容,但这是从缓存中提供页面,然后获取请求。因此,如果服务器端发生了任何更改,它不会在第一次打开页面时反映出来,而是在第二次访问时显示更改。

所以我想要的是,如果颤动从服务器获取304 response,它将从缓存中获取,否则应从提取的数据中获取。却没有发生。

response.header也没有像200304那样显示响应代码,因此抖动可以从缓存中获取或投放。

正在使用的实际代码如下:

Future<MyUserProfile> fetchMyUserProfile() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  final userid = prefs.getString('user_id');
  var userProfile =  await DefaultCacheManager().getSingleFile("url");        
  final response = await userProfile.readAsString();



 if (response != '') {
    print(userProfile);
    // If the server did return a 200 OK response,
    // then parse the JSON.
    return MyUserProfile.fromJson(json.decode(response));
  } else {
    // If the server did not return a 200 OK response,
    // then throw an exception.
    throw Exception('Failed to load Profile');
  }

}

1 个答案:

答案 0 :(得分:1)

不要无礼,但是您正在寻找的信息很容易找到...也就是说,您在正确的地方查找,例如官方文档。

https://api.flutter.dev/flutter/dart-io/HttpResponse-class.html

HttpResponse类...标头→HttpHeaders返回响应 标头。 [...]只读

http.Response response = await http.get(url...
print(response.headers);

编辑:回答添加到原始问题中的子问题。

要检查状态码是什么,您只需通过response.statusCode

示例:

  http.Response response = await http.get(url...
  if (response.statusCode == 200) {
    // do something
  } else if (response.statusCode == 304) {
    // do something else
  } else {
    // handle this
  }