2

そこで、次のようなコントローラーを作成しました。

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

私のルートは次のようなものです:

get "howdy/show"

そして私の見解はこれです:

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

しかし、私が行くとhttp://localhost:3000/howdy/show、このエラーが発生します

HTTP リクエスト パスが空です

私は Net::HTTP にまったく慣れていないので、動作するシンプルなものを作成しようとしています!

4

2 に答える 2

4

doを使用してリクエストを送信するにnet/httpは:

        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)
于 2013-02-22T19:00:11.910 に答える
4

最初の引数として URL を使用get_responseして渡し、2 番目の引数としてスラッシュを使用してパスを作成できます。

@resp = Net::HTTP.get_response("www.google.com", "/")
于 2013-02-22T18:59:05.777 に答える