Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check HTTP Status Code in Rails

Is there an easy way to check the status code of another website (e.g. http://google.com) in Ruby on Rails?

Ideally, the list of URLs to check would be pulled from a DB and processed via CRON (or equivalent).

like image 630
Trent Scott Avatar asked Jan 18 '23 17:01

Trent Scott


1 Answers

require 'net/http'

http = Net::HTTP.new('www.google.com',80)
response = http.request_get('/')
p response.status

Slightly more efficient (traffic wise) might be:

response = http.request_head('/')
p response.status

Source: http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTP.html#M001403

like image 198
ebeland Avatar answered Jan 28 '23 16:01

ebeland