views:

912

answers:

2

Hi,

I want to autodetect the pingback-url of remote websites, so I need to parse the HTTP response headers sent by the remote server. I don't need and don't want the contents of the remote website, I'm only looking for something like this:

X-Pingback: http://www.techcrunch.com/xmlrpc.php

Similar to using curl:

curl -I "your url"

Is there a way to do this with rails? When using open-uri I can only get the contents but not the headers.

Thank you! Ole

+3  A: 

This is not a Rails specific question, but it can be solved with the core Ruby API.

require 'net/http'

url = URI.parse('http://www.google.ca/')
req = Net::HTTP::Head.new(url.path)

res = Net::HTTP.start(url.host, url.port) {|http|
   http.request(req)
}

puts res['X-Pingback']
> "http://www.techcrunch.com/xmlrpc.php"
BigCanOfTuna
cool, thanks a lot!
ole_berlin
+2  A: 

If you don't mind using a gem, you might try Patron, which is a ruby wrapper for the libcurl library. Usage might look something like this:

sess = Patron::Session.new
sess.timeout = 10
sess.base_url = "http://myserver.com:9900"
resp = sess.get( "your url" )
headers = resp.headers
John Hyland