Search code examples
rubynet-http

How to use Net::HTTP's `get_print` method


I'm trying to create and initialize an instance of Net::HTTP as HTTPClient. When I get to the get_print method, though, it is telling me that it doesn't exist. What am I doing incorrectly?

require 'Net/HTTP'

print "Enter address for HTTP request: "
URL = gets.chomp
puts ''

HTTPClient = Net::HTTP.new(URL, 80)

HTTPClient.get_print URI(URL)

Solution

  • You've stored an instance of the classNet::HTTP in your HTTPClient constant. But get_print isn't an instance method of Net::HTTP (see the docs), it's a singleton method of the class object Net::HTTP itself. Therefore, you cannot call get_print on an instance.

    Here's how you might use get_print appropriately:

    require 'net/http'
    
    Net::HTTP.get_print('www.google.com', '/')