close

How to print out http-response header in Python

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to print out http-response header in Python in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to print out http-response header in Python?

  1. How to print out http-response header in Python?

    The dictionary is special, though: it's made just for HTTP headers. According to RFC 7230, HTTP Header names are case-insensitive.

  2. print out http-response header in Python

    The dictionary is special, though: it's made just for HTTP headers. According to RFC 7230, HTTP Header names are case-insensitive.

Method 1

We can view the server’s response headers using a Python dictionary:

>>> r.headers
{
    'content-encoding': 'gzip',
    'transfer-encoding': 'chunked',
    'connection': 'close',
    'server': 'nginx/1.0.4',
    'x-runtime': '148ms',
    'etag': '"e1ca502697e5c9317743dc078f67693f"',
    'content-type': 'application/json'
}

And especially the documentation notes:

The dictionary is special, though: it’s made just for HTTP headers. According to RFC 7230, HTTP Header names are case-insensitive.

So, we can access the headers using any capitalization we want:

and goes on to explain even more cleverness concerning RFC compliance.

Using Response.iter_content will handle a lot of what you would otherwise have to handle when using Response.raw directly. When streaming a download, the above is the preferred and recommended way to retrieve the content.

It offers as example:

>>> r = requests.get('https://api.github.com/events', stream=True)
>>> r.raw
<requests.packages.urllib3.response.HTTPResponse object at 0x101194810>
>>> r.raw.read(10)
'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03'

But also offers advice on how to do it in practice by redirecting to a file etc. and using a different method:

Using Response.iter_content will handle a lot of what you would otherwise have to handle when using Response.raw directly

Method 2

Here’s how you get just the response headers using the requests library like you mentioned (implementation in Python3):

import requests

url = "https://www.google.com"
response = requests.head(url)
print(response.headers) # prints the entire header as a dictionary
print(response.headers["Content-Length"]) # prints a specific section of the dictionary

It’s important to use .head() instead of .get() otherwise you will retrieve the whole file/page like the rest of the answers mentioned.

If you wish to retrieve a URL that requires authentication you can replace the above response with this:

response = requests.head(url, auth=requests.auth.HTTPBasicAuth(username, password))

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read