How to get HTTP redirect redirect url?

I write on Python client modules to check if HTTP 302 is redirected to my Google App Engine site on the right pages. So far I have called urllib2.urlopen(my_url).geturl() . However, I ran into two problems:

  • The URL returned by geturl () does not include URL query strings such as ?k1=v1&k2=v2 ; how can i see them? (I need to check if I passed the query string of the visitor's original URL correctly to the redirect page.)
  • geturl() shows the final URL after any additional redirects. I just care about the first redirect (one of my sites); After that, I don’t feel anything. For example, let's say my site is example.com . If the user requests http://www.example.com/somepath/?q=foo , I can redirect them to http://www.anothersite.com?q=foo . This other site may make another redirect to http://subdomain.anothersite.com?q=foo , which I cannot control or predict. How can I make sure my call forwarding is correct?
+3
source share
2 answers

Put follow_redirects=False in the fetch function, then find the location of the first redirect from the "location" header in the response, like this:

 response = urlfetch.fetch(your_url, follow_redirects=False) location = response.headers['Location'] 
+4
source

Use httplib (and look at the return status and response location header) to avoid “automatic redirects,” which impedes your testing. Here is a good example here .

+5
source

Source: https://habr.com/ru/post/1303033/


All Articles