Selected Reading

Python response.headers attribute



Response.headers attribute of the Python Requests module is a dictionary-like object containing the response headers received from the server.

These headers provide metadata about the response such as content type ('Content-Type'), content length ('Content-Length'), server information ('Server') and caching directives ('Cache-Control'). They help clients to interpret the response correctly, manage caching, handle authentication ('WWW-Authenticate') and understand server capabilities ('Allow', 'Accept-Ranges').

Accessing a specific header is straightforward using the header name as a key such as 'response.headers['Content-Type']'. This metadata is crucial for effective HTTP communication and resource handling.

Syntax

Following is the syntax and parameters of Response.headers attribute of the Python Requests module −

response.headers

Parameter

This attribute does not accept any parameters.

Return value

This attribute returns a dictionary-like object that contains all the headers.

Example 1

Following is the example of Response.headers attribute of the Python Requests module which sends a GET request to the specified URL and print out all the headers in the response −

import requests

# Make a request to a website
response = requests.get('https://www.tutorialspoint.com')

# Print all headers
for header, value in response.headers.items():
    print(f'{header}: {value}')

Output

Cache-Control: max-age=2592000
Content-Type: text/html; charset=iso-8859-1
Date: Fri, 28 Jun 2024 06:22:26 GMT
Expires: Sun, 28 Jul 2024 06:22:26 GMT
Server: Apache/2.4.59 (Ubuntu)
Strict-Transport-Security: max-age=63072000; includeSubDomains
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Version: OCT-10 V1
Content-Length: 199

Example 2

To access a specific header from the response using the requests library in Python we can use the header's name as a key in the headers dictionary. Here is the example of it −

import requests

# Make a request to a website
response = requests.get('https://www.tutorialspoint.com')

# Access a specific header
content_type = response.headers.get('Content-Type')

# Print the specific header
print(f'Content-Type: {content_type}')

Output

Content-Type: text/html; charset=iso-8859-1

Example 3

In this example we check for the presence of a specific header in the response using the requests library in Python and we use the in operator. −

import requests

# Make a request to a website
response = requests.get('https://www.tutorialspoint.com')

# Check for the presence of a specific header
header_name = 'Content-Type'
if header_name in response.headers:
    print(f'{header_name} header is present.')
    # Access and print the specific header value
    header_value = response.headers[header_name]
    print(f'{header_name}: {header_value}')
else:
    print(f'{header_name} header is not present.')

Output

Content-Type header is present.
Content-Type: text/html; charset=iso-8859-1
python_modules.htm
Advertisements