Search code examples
ruby-on-railscurlheaderpingback

Rails: How to get the servers response HTTP headers?


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


Solution

  • 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"