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
...

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.