How to get a long/actual url from a short url?

Sometimes when we try to fetch short urls, using curl, we get a blank page as reply or a 301 ERROR. The reason being that there is no html file stored at that location,only a header with details ,on where the person who has opened the url would be redirected to.

So to get the actual url or long url from the short url, we just have to read the location stored in the html header.

1
2
3
4
require 'net/http'

uri = URI('http://bit.ly/ZPsGIF')
data = Net::HTTP.get_response(uri)

To get the location data, and for easy usage we convert this object to as hash.

data.to_hash, now running the command data["location"] will provide you with the long url.

Combining all the above codes the actual program would be :

1
2
3
4
5
require 'net/http'

uri = URI('http://bit.ly/ZPsGIF')
data = Net::HTTP.get_response(uri).to_hash
puts data["location"]