Skip to content
Related Articles

Related Articles

Improve Article

HEAD method – Python requests

  • Last Updated : 26 Feb, 2020
Geek Week

Requests library is one of the important aspects of Python for making HTTP requests to a specified URL. This article revolves around how one can make HEAD request to a specified URL using requests.head() method. Before checking out the HEAD method, let’s figure out what a Http HEAD request is –

HEAD Http Method

HEAD is a request method supported by HTTP used by the World Wide Web. The HEAD method asks for a response identical to that of a GET request, but without the response body. This is useful for retrieving meta-information written in response headers, without having to transport the entire content.

How to make HEAD request through Python Requests

Python’s requests module provides in-built method called head() for making a HEAD request to a specified URI.

Syntax –

requests.head(url, params={key: value}, args)

Example –



Let’s try making a request to httpbin’s APIs for example purposes.




import requests
  
# Making a HEAD request
r = requests.head('https://httpbin.org/', data ={'key':'value'})
  
# check status code for response recieved
# success code - 200
print(r)
  
# print headers of request
print(r.headers)
  
# checking if request contains any content
print(r.content)

save this file as request.py and through terminal run,

python request.py

Output –

head-method-python-requestts

Advanced with HEAD request

The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request. This method can be used for obtaining metainformation about the entity implied by the request without transferring the entity-body itself. This method is often used for testing hypertext links for validity, accessibility, and recent modification.

The response to a HEAD request MAY be cacheable in the sense that the information contained in the response MAY be used to update a previously cached entity from that resource. If the new field values indicate that the cached entity differs from the current entity (as would be indicated by a change in Content-Length, Content-MD5, ETag or Last-Modified), then the cache MUST treat the cache entry as stale.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :