Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Net::HTTP get source code and status

Tags:

ruby

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.

like image 403
agmcleod Avatar asked Jun 09 '11 19:06

agmcleod


People also ask

What are status code 2xx 3xx 4xx 5xx in API?

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.

What is HTTP status code1?

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.

How do I return my 204 status code?

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.


2 Answers

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.

like image 119
the Tin Man Avatar answered Nov 03 '22 06:11

the Tin Man


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 > 
like image 39
agmcleod Avatar answered Nov 03 '22 08:11

agmcleod