Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the contents of an http request in Ruby?

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.

like image 492
Ryan Florence Avatar asked Aug 27 '09 04:08

Ryan Florence


People also ask

What are the parts of an HTTP request message?

An HTTP request is divided into three parts: Request line, header and body.

What does a GET request contain?

The GET method is used to retrieve information from the given server using a given URI. Requests using GET should only retrieve data and should have no other effect on the data.


2 Answers

Using the net/http library as shown:

require 'net/http'

response = Net::HTTP.get_response('mysite.com','/api/v1/messages')
p response.body
like image 52
Mullins Avatar answered Sep 16 '22 15:09

Mullins


require 'net/http'
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!

like image 42
Ryan Florence Avatar answered Sep 18 '22 15:09

Ryan Florence