Check web request status code : HTTP Post « Network « Ruby






Check web request status code


require 'net/http'         # The library we need
host = 'www.example.com'   # The web server
path = '/index.html'       # The file we want

http = Net::HTTP.new(host)      # Create a connection
headers, body = http.get(path)  # Request the file
if headers.code == "200"        # Check the status code
                                # NOTE: code is not a number!
  print body                    # Print body if we got it
else                            # Otherwise
  puts "#{headers.code} #{headers.message}" # Display error message
end

 








Related examples in the same category

1.Posting Form Data
2.Set form data
3.Net::HTTP:
4.Get random number from random.org