Per documentation of requests.get
we have:
By default Requests will perform location redirection for all verbs except HEAD.
So how would one write the following function without the actual GET operation (I discard the content anyway):
def get_location(url):
response = requests.get(url) # HTML content is discarded
return response.url
I did verify using curl -I
a HEAD operation seems to contains the new location:
$ curl -I 'https://www.example.org/redirect.php?val=1'
HTTP/1.1 301 Moved Permanently
Cache-Control: private
Content-Length: 0
Content-Type: text/html
Location: https://www.acme.corp/foobar.html
...