Last modified: Nov 12, 2024 By Alexander Williams
Python requests.HEAD(): Guide to HTTP Header Requests
The requests.HEAD()
method in Python is a powerful tool for retrieving HTTP headers from a URL without downloading the actual content. This makes it incredibly efficient for checking resource metadata.
Understanding requests.HEAD()
Unlike GET requests, HEAD requests only fetch headers. This is particularly useful when you want to check a resource's existence or metadata without downloading its contents.
Basic Usage
import requests
response = requests.head('https://api.github.com')
print(response.headers)
{'Server': 'GitHub.com', 'Date': 'Thu, 01 Dec 2023 12:00:00 GMT', 'Content-Type': 'application/json'}
Checking Resource Size
One common use case is checking file sizes before downloading, especially useful when dealing with image files.
import requests
url = 'https://example.com/large-file.pdf'
response = requests.head(url)
file_size = response.headers.get('content-length', 0)
print(f"File size: {int(file_size) / 1024 / 1024:.2f} MB")
Checking Resource Availability
You can use HEAD requests to verify if a resource exists by checking the status code without downloading the entire content.
import requests
def check_url(url):
try:
response = requests.head(url)
return response.status_code == 200
except requests.RequestException:
return False
url = 'https://api.github.com'
print(f"URL is available: {check_url(url)}")
Using Custom Headers
Like other request methods such as POST or PUT, you can add custom headers to your HEAD request.
headers = {'User-Agent': 'Mozilla/5.0'}
response = requests.head('https://api.github.com', headers=headers)
Error Handling
Always implement proper error handling when making HEAD requests to handle potential network issues or invalid URLs.
try:
response = requests.head('https://api.github.com')
response.raise_for_status()
except requests.exceptions.RequestException as e:
print(f"Error occurred: {e}")
Conclusion
The requests.HEAD() method is an efficient way to retrieve HTTP headers without downloading content. It's perfect for checking resource metadata, file sizes, and availability before making full requests.