2

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!

2 Answers2

4

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", "/")
Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
fmendez
  • 7,250
  • 5
  • 36
  • 35
4

To send a request using net/http do:

        uri = URI.parse("http://www.google.com")
        http = Net::HTTP.new(uri.host, uri.port)
        request = Net::HTTP::Get.new(uri.request_uri)
        #This makes the request
        resp = http.request(request)
BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156