Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Does dart has get_headers($url) function?

In php you can retrive only headers without body

function UR_exists($url){
   $headers=get_headers($url);
   return stripos($headers[0],"200 OK")?true:false;
}

How I can do this in dart?

Maybe some of http.get() with anyone parameters in headers can help?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could get the headers this way:

final response = await http.get(yourUri, headers: {'method': 'HEAD'});

print(response.headers);

If you want, you can of course wrap that in a function that only returns the headers, so it will be similar to the command you are used to. Something like this (you would have to make it more robust for handling errors, failed requests etc…):

Future<Map<String, String>> getHeaders(Uri uri) async {
  return (await http.get(uri, headers: {'method': 'HEAD'})).headers;
}

So running it like this:

print(await getHeaders(Uri.parse('http://www.google.com')));

Will print (in my case):

{set-cookie: AEC=datadatadata; expires=Wed, 10-May-2023 13:17:58 GMT;
path=/; domain=.google.com; Secure; HttpOnly; SameSite=lax,
cache-control: private, max-age=0, date: Fri, 11 Nov 2022 13:17:58
GMT, content-encoding: gzip, content-length: 6747, x-frame-options:
SAMEORIGIN, content-type: text/html; charset=ISO-8859-1,
x-xss-protection: 0, server: gws, expires: -1}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading