user1899082
user1899082

Reputation:

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!

Upvotes: 2

Views: 2559

Answers (2)

fmendez
fmendez

Reputation: 7338

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", "/")

Upvotes: 4

BlackHatSamurai
BlackHatSamurai

Reputation: 23483

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)

Upvotes: 4

Related Questions