【问题标题】:HTTP request path is empty in working with Net::HTTP使用 Net::HTTP 时 HTTP 请求路径为空
【发布时间】:2013-02-08 11:38:58
【问题描述】:

所以我刚刚创建了一个这样的控制器:

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 完全陌生,只是想创造一些简单的东西!

【问题讨论】:

    标签: ruby-on-rails ruby net-http


    【解决方案1】:

    您可以使用get_response 并将 URL 作为第一个参数传递,并将斜杠作为第二个参数传递:

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

    【讨论】:

      【解决方案2】:

      使用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-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-28
        • 1970-01-01
        • 2013-06-14
        • 1970-01-01
        • 2023-03-27
        相关资源
        最近更新 更多