Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get long URL from short URL?

Using Ruby, how do I convert the short URLs (tinyURL, bitly etc) to the corresponding long URLs?

like image 551
user61734 Avatar asked May 23 '09 18:05

user61734


2 Answers

I don't use Ruby but the general idea is to send an HTTP HEAD request to the server which in turn will return a 301 response (Moved Permanently) with the Location header which contains the URI.

HEAD /5b2su2 HTTP/1.1
Host: tinyurl.com
Accept: */*

RESPONSE:

HTTP/1.1 301 Moved Permanently
Location: http://stackoverflow.com
Content-type: text/html
Date: Sat, 23 May 2009 18:58:24 GMT
Server: TinyURL/1.6

This is much faster than opening the actual URL and you don't really want to fetch the redirected URL. It also plays nice with the tinyurl service.

Look into any HTTP or curl APIs within ruby. It should be fairly easy.

like image 156
aleemb Avatar answered Oct 05 '22 18:10

aleemb


You can use the httpclient rubygem to get the headers

#!/usr/bin/env ruby

require 'rubygems'
require 'httpclient'

client = HTTPClient.new

result = client.head(ARGV[0])
puts result.header['Location']
like image 24
slillibri Avatar answered Oct 05 '22 19:10

slillibri