Yes, you can access the response headers using the Requests library in Python. When you make a request to a web server using the Requests library, the server responds with not only the content you requested but also a set of HTTP headers. These headers contain metadata about the response, such as content type, server information, content length, and more.
Here is a basic example of how to access the response headers using Requests:
import requests
# Make a GET request to a URL
response = requests.get('https://httpbin.org/get')
# Access the response headers
headers = response.headers
# Print the header keys and values
for key, value in headers.items():
print(f'{key}: {value}')
# You can also access specific headers directly
content_type = headers['Content-Type']
print(f'Content-Type: {content_type}')
It's worth noting that the headers object returned by the Requests library is case-insensitive, which means you can access header fields without worrying about their case:
# Both of these will work and return the same result
content_type = headers['content-type']
content_length = headers['Content-Length']
If you try to access a header that isn't present in the response, it's a good idea to use the get
method to avoid a KeyError
. The get
method returns None
if the header is not found:
# This won't raise an error even if the header is not found
server_header = headers.get('Server')
if server_header:
print(f'Server: {server_header}')
else:
print('The "Server" header is not present in the response.')
Keep in mind that headers are a crucial part of the HTTP protocol and understanding their values can be important for tasks such as handling sessions, dealing with content types, managing redirections, and more.