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.
2xx successful – the request was successfully received, understood, and accepted. 3xx redirection – further action needs to be taken in order to complete the request. 4xx client error – the request contains bad syntax or cannot be fulfilled. 5xx server error – the server failed to fulfil an apparently valid request.
Prevailing theory is that the status is set to null and the statuscode set to -1 when the response object is constructed, and then something happens to the connection that means the request doesn't complete, so these defaults are never overwritten with real values.
The 204 (No Content) status code indicates that the server has successfully fulfilled the request and that there is no additional content to send in the response payload body. While 200 OK being a valid and the most common answer, returning a 204 No Content could make sense as there is absolutely nothing to return.
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.
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 >
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With