agmcleod
agmcleod

Reputation: 13621

Net::HTTP get source code and status

I am currently getting the source code of the page using:

Net::HTTP.get(URI.parse(page.url))

I would also like to get the HTTP status, without making a second request.

Is there a way to do that with another method? I've been looking at the documentation, but cannot seem to find what I am looking for.

Upvotes: 5

Views: 5898

Answers (2)

the Tin Man
the Tin Man

Reputation: 160571

In my opinion, unless you need some real low level access or control, you're better off using Ruby's built-in Open::URI module:

require 'open-uri'
io = open('http://www.example.org/') #=> #<StringIO:0x0000010103e240>
body = io.read[0, 50] #=> "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Trans"
io.status #=> ["200", "OK"]
io.base_uri #=> #<URI::HTTP:0x00000100bf2ad8 URL:http://www.iana.org/domains/example/>

Notice that the output of base_uri is different from the URL I passed in. Open::URI follows redirects for you, which Net::HTTP will not do. That can pay off big time if you're throwing a lot of random URLs at your code and don't want to have to write the redirect handler.

Upvotes: 7

agmcleod
agmcleod

Reputation: 13621

Sorry, actually figured it out :).

ruby-1.9.2-p136 :004 > r = Net::HTTP.get_response(URI.parse('http://badurlexample.com')) 
 => #<Net::HTTPInternalServerError 500 Internal Server Error readbody=true> 
ruby-1.9.2-p136 :005 > r.inspect
 => "#<Net::HTTPInternalServerError 500 Internal Server Error readbody=true>" 
ruby-1.9.2-p136 :006 > r.body
 => "1 Errors:\r\nLine: 40 - ; expected" 
ruby-1.9.2-p136 :007 > 

Upvotes: 3

Related Questions