Home > Software design >  Getting Specific Header value from Head call response in Flutter
Getting Specific Header value from Head call response in Flutter

Time:11-02

I am performing Head call as below using http:

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());
  });
}

The values which I am getting from Header is as below :

  1. For print(value.hedaers.toString()),

{x-served-by: psm100.akshar-dev.ml, connection: keep-alive, last-modified: Thu, 13 Oct 2022 00:09:35 GMT, accept-ranges: bytes, date: Wed, 02 Nov 2022 10:24:35 GMT, content-length: 69910, etag: "6347573f-11116", content-type: application/json, server: openresty}

  1. For print(value.headers.values.toString()),

(psm100.akshar-dev.ml, keep-alive, Thu, 13 Oct 2022 00:09:35 GMT, ..., application/json, openresty)

I want specific header value i.e. The value for last-modified key. How can I get it?

CodePudding user response:

Try the following code

value.hedaers['last-modified']

CodePudding user response:

have you parse json after getting respose?

you can do this.

if (response.statusCode == 200) {
  return Album.fromJson(jsonDecode(response.body));
} else {
  throw Exception('Failed to load album');
}

and ALbum is class you can define it as per response

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'],
    );
  }
}

for making this yupe of class automatically you can use either extension "JSON to DART" or user online tool

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,
        };
    }
  • Related