0

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
...
malat
  • 12,152
  • 13
  • 89
  • 158

1 Answers1

0

Right after posting my question, I stumble upon:

So this worked for me:

def get_location(url):
  response = requests.head(url, allow_redirects=True)
  return response.url
malat
  • 12,152
  • 13
  • 89
  • 158