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

  • submit to reddit
require 'uri'
require 'net/http'
url = ""
r = Net::HTTP.get_response(URI.parse(url).host, URI.parse(url).path)

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


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

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

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

whups, I meant get_response, of course.

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

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