Requests Head method:

The head() method of requests module sends a request for data to a web server. Therefore, the response object it returns contains the response headers without including the content itself. In other words, the head() method is same as the requests get() method but excludes the detailed information. Thus, it is much more faster than get method. So, this can be used when we need to check for a particular resource on the web and type of content it returns without worrying about the content itself.

The common code returned by response object is as shown below :

  • 301 – Moved Permanently. Indicates that resource has been moved permanently to the URL given by the location headers.
  • 302 – Found : Moved Temporarily. Indicates that resource has been moved temporarily to the URL given by the location headers.
  • 200 – OK

Method Signature

The signature for the head method is as shown below.

requests.head(url, args)

Method parameters and return type

  • Here, the head method takes two parameters as listed below.
    • url – Mandatory parameter. It specifies the URL
    • args – It can pass several arguments like cookies, headers, proxies, timeout, stream, auth and cert. These arguments are optional.
  • However, it returns a response object.

Python head method Examples:

Example 1: In this case, we will send requests to a URL and print the status code and reason of its response for two different URLs.

#Importing requests module
import requests

# variable storing url 
url="https://google.com"
url1="https://irctc.co.in"

# Making a head request
r = requests.head(url)
r1 = requests.head(url1)  

# Checking status code and reason of response 
print("Status code for Google- ",r.status_code)
print("Reason for Google - ",r.reason)
print("Status code for IRCTC - ",r1.status_code)
print("Reason for IRCTC - ",r1.reason)

Output

Status code for Google - 301
Reason for Google- Moved Permanently
Status code for IRCTC - 302
Reason for IRCTC - Moved Temporarily

Example 2: In this case,we will use the header parameter to set the HTTP headers on the given URL. Here, we will use the URL page that does not exist. Hence, it will return the code for page moved permanently.

#Importing requests module
import requests

# variable storing url 
url = "http://nhk.e6a.mytemp.website/perl"

#Using the headers parameter to set the HTTP headers:
h = {"HTTP_HOST": "onlinetut", "Content-Type": "application/json" }
x = requests.head(url, headers = h)

print("Status code - ",x.status_code)
print("Reason - ",x.reason)

Output

Status code - 301
Reason - Moved Permanently

Example 3: In this case, this method returns the response’s status if it arrives before timeout. However, if not, it will raise the ReadTimeoutException.

#Importing requests module
import requests

# variable storing url 
url = "http://nhk.e6a.mytemp.website/python"

#Using the timeout parameter 
x = requests.head(url, timeout=10)

print("Status code - ",x.status_code)
print("Reason - ",x.reason)

Output

Status code - 301
Reason - Moved Permanently

Conclusion

Hence, the head() method can check to confirm that website is responding, and verify the content type, without actually downloading the whole content.

References

Happy Learning 🙂