首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从颤振中的头部呼叫响应中获取特定的标头值

从颤振中的头部呼叫响应中获取特定的标头值
EN

Stack Overflow用户
提问于 2022-11-02 10:29:42
回答 2查看 35关注 0票数 1

我正在使用http执行以下呼叫:

代码语言:javascript
复制
Future<void> fetchModifiedDate(String fileUrl,BuildContext context) async{
  if (await checkNetworkConnection(context)) {
  var responseValue = [];
  var response = http.head(Uri.parse(fileUrl));
  response.then((value) {
    responseValue = value.headers.values.toString().split(",");
    modifiedDate = responseValue[1].trim();
    print(value.headers.toString());
    print(value.headers.values.toString());
  });
}

我从标题中得到的值如下:

  1. 用于打印(value.hedaers.toString()),

{x-by: psm100.akshar-dev.ml,连接:保持活动,最后修改:清华,2022年10月13日:09:35 GMT,接受范围:字节,日期:10,011月2日10:24:35 GMT,内容长度: 69910,etag:"6347573f-11116",内容类型: application/json,server: openresty}

  1. 用于打印(value.headers.values.toString()),

(psm100.akshar-dev.ml,保持活力,清华,2022年10月13日:09:35格林尼治时间,.,应用/json,开放)

我想要特定的头值,即last-modified键的值。我怎么才能拿到呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-11-02 10:42:10

尝试以下代码

代码语言:javascript
复制
value.hedaers['last-modified']
票数 3
EN

Stack Overflow用户

发布于 2022-11-02 10:45:27

你有在得到答复后解析json吗?

你能做到的。

代码语言:javascript
复制
if (response.statusCode == 200) {
  return Album.fromJson(jsonDecode(response.body));
} else {
  throw Exception('Failed to load album');
}

ALbum是类,您可以根据响应来定义它

代码语言:javascript
复制
class Album {
  final int userId;
  final int id;
  final String title;

  const Album({
    required this.userId,
    required this.id,
    required this.title,
  });

  factory Album.fromJson(Map<String, dynamic> json) {
    return Album(
      userId: json['userId'],
      id: json['id'],
      title: json['title'],
    );
  }
}

要自动生成这个类的yupe,可以使用扩展名"JSON to DART“或用户在线工具

代码语言:javascript
复制
class Response {
        Response({
            this.xServedBy,
            this.connection,
            this.lastModified,
            this.acceptRanges,
            this.date,
            this.contentLength,
            this.etag,
            this.contentType,
            this.server,
        });
    
        String xServedBy;
        String connection;
        String lastModified;
        String acceptRanges;
        String date;
        int contentLength;
        String etag;
        String contentType;
        String server;
    
        factory Response.fromJson(Map<String, dynamic> json) => Response(
            xServedBy: json["x-served-by"],
            connection: json["connection"],
            lastModified: json["last-modified"],
            acceptRanges: json["accept-ranges"],
            date: json["date"],
            contentLength: json["content-length"],
            etag: json["etag"],
            contentType: json["content-type"],
            server: json["server"],
        );
    
        Map<String, dynamic> toJson() => {
            "x-served-by": xServedBy,
            "connection": connection,
            "last-modified": lastModified,
            "accept-ranges": acceptRanges,
            "date": date,
            "content-length": contentLength,
            "etag": etag,
            "content-type": contentType,
            "server": server,
        };
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74287736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档