views:

523

answers:

3

In PHP I can do this:

$request = "http://www.example.com/someData";
$response = file_get_contents($request);

How would I do the same thing in Ruby (or some Rails method?)

I've been googling for a half an hour and coming up completely short.

A: 

In your view try

<%= request.inspect %>
Mike Buckbee
Hmm ... maybe I wasn't clear, I'm trying to get something external, not in my app.
rpflo
I was reading it as "How do I get the contents of a request object", not the contents of a web page, my bad.
Mike Buckbee
+3  A: 
Net::HTTP.get(URI.parse('http://www.example.com/index.html'))

Not sure why I didn't find this earlier. Unless there's an better way, I'm going with this!

rpflo
+2  A: 

The standard library package open-uri is what you're after:

require 'open-uri'
contents = open('http://www.example.com') {|io| io.read}
# or
contents = URI.parse('http://www.example.com').read
glenn jackman
How is this different than Net::HTTP?
rpflo
In this case, not much. However that package does provide many useful shortcuts. Read the docs at http://www.ruby-doc.org/stdlib/
glenn jackman