Get Response Headers in Python Requests
If you're working with APIs or scraping websites, you may need to access the response headers of a request made in Python. Thankfully, the requests
library makes this easy.
Method 1: Accessing Headers Directly
To get the headers of a response, you can simply access the headers
attribute of the response object:
import requests
response = requests.get('https://www.example.com')
headers = response.headers
print(headers)
This will print out a dictionary of headers:
{'Date': 'Sat, 10 Oct 2020 00:00:00 GMT',
'Server': 'Apache/2.4.29 (Ubuntu)',
'Content-Length': '12345',
'Content-Type': 'text/html; charset=UTF-8',
...
}
You can access individual headers by using the header name as a key:
print(headers['Content-Type'])
This will print out the value of the Content-Type
header:
text/html; charset=UTF-8
Method 2: Using the Response's iter_lines()
Method
If you're working with a large response that has many headers, you may want to access the headers one at a time instead of loading them all into memory at once. To do this, you can use the iter_lines()
method of the response object:
import requests
response = requests.get('https://www.example.com', stream=True)
for line in response.iter_lines():
if line:
print(line.decode('utf-8'))
else:
break
This will print out the headers one at a time:
Date: Sat, 10 Oct 2020 00:00:00 GMT
Server: Apache/2.4.29 (Ubuntu)
Content-Length: 12345
Content-Type: text/html; charset=UTF-8
...
You can then parse the headers as needed.
Conclusion
There are several ways to get response headers in Python using the requests
library. Whether you use the headers
attribute directly or iterate over the headers using iter_lines()
, you should be able to access the information you need.