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?
CodePudding user response:
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}