DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Fetch A URL In Ruby

07.07.2005
| 26057 views |
  • submit to reddit
        
require 'uri'
require 'net/http'
url = "http://www.whatever.com/whatever.txt"
r = Net::HTTP.get_response(URI.parse(url).host, URI.parse(url).path)

r.code = 200 | 404 | 500, etc
r.body = *text of page*    

Comments

Anonymous ˙ˆ∆∂ˆßå replied on Thu, 2006/09/14 - 11:14pm

Let's not forget open-uri guys. :) require 'open-uri' puts open("http://jicksta.com").read

Snippets Manager replied on Mon, 2012/05/07 - 2:13pm

whups, I meant get_response, of course.

Snippets Manager replied on Mon, 2012/05/07 - 2:13pm

It's easier to do: r = Net::HTTP.get_reponse( URI.parse( url ) ) rather than parse twice and pull out the important bits.