HEAD method - Python requests
Last Updated :
09 Dec, 2021
Improve
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 -
Python3
save this file as request.py and through terminal run,
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 received
# success code - 200
print(r)
# print headers of request
print(r.headers)
# checking if request contains any content
print(r.content)
python request.pyOutput -
