vishnu
vishnu

Reputation: 451

URL Redirection not working

The following is the code I use,

import unshortenit 

unshortened_uri,status = unshortenit.unshorten('http://4sq.com/1iyfyI5')

print unshortened_uri

print status

The following is the output:

https://foursquare.com/edtechschools/checkin/53ac1e5f498e5d8d736ef3be?s=BlinbPzgFfShr0vdUnbEJUnOYYI&ref=tw

Invalid URL u'/tanyaavrith/checkin/53ac1e5f498e5d8d736ef3be?s=BlinbPzgFfShr0vdUnbEJUnOYYI&ref=tw': No schema supplied

whereas if I use the same url in browser, it correctly redirects to the actual url. Any idea why its not working ?

Upvotes: 0

Views: 137

Answers (1)

Leonardo.Z
Leonardo.Z

Reputation: 9801

There's a 301 redirect chain:

From:
'http://4sq.com/1iyfyI5'

To:
'https://foursquare.com/edtechschools/checkin/53ac1e5f498e5d8d736ef3be?s=BlinbPzgFfShr0vdUnbEJUnOYYI&ref=tw'

To:
'/tanyaavrith/checkin/53ac1e5f498e5d8d736ef3be?s=BlinbPzgFfShr0vdUnbEJUnOYYI&ref=tw'

unshortenit use requests, and requests can't understand the last relative url.

Updates:

Actually, request lib can handle http redirects well and automatically with request.get method.

e.g.

import requests
r=requests.get('http://4sq.com/1iyfyI5')
r.status_code # 200
r.url # u'https://foursquare.com/tanyaavrith/checkin/53ac1e5f498e5d8d736ef3be?s=BlinbPzgFfShr0vdUnbEJUnOYYI&ref=tw'

But unshortenit does not want the overhead of HTTP GET, instead it uses HTTP HEAD. If the response of the HTTP Head request has a 'Location'field in its header, unshortenit makes a new HTTP HEAD request to that location. The new request is isolated from the original request, and relative url doesn't work any more.

Reference (from Wikipedia):

While the obsolete IETF standard RFC 2616 (HTTP 1.1) requires a complete absolute URI for redirection, the most popular web browsers tolerate the passing of a relative URL as the value for a Location header field. Consequently, the current revision of HTTP/1.1 makes relative URLs conforming

Upvotes: 1

Related Questions