Search code examples
ruby-on-railsrubynet-http

HTTP request path is empty in working with Net::HTTP


So I jut created a controller like this:

require 'net/http'
class HowdyController < ApplicationController
  def show
    url = URI.parse("http://google.com")
    req = Net::HTTP::Get.new(url.path)
    @resp = Net::HTTP.new(url.host, url.port).start {|http| http.request(req)}
  end
end

and my route is like this:

get "howdy/show"

and my view is lie this:

<h1>Howdy#show</h1>
<%= "The call to example.com returned this: #{@resp}" %>

But when I go to http://localhost:3000/howdy/show I get this error

HTTP request path is empty

I am totally new to Net::HTTP and just trying to create something simple that works!


Solution

  • You can use get_response and pass the URL as the first argument, and a slash as the second one to make the path:

    @resp = Net::HTTP.get_response("www.google.com", "/")