2013-02-22 2 views
2

그래서 내가 만든 돌출이 같은 컨트롤러 :HTTP 요청 경로가 인터넷 : HTTP 작업에 비어

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 요청 경로가 비어 있습니다

내가 HTTP : 인터넷 바로 작동하는 간단한 뭔가를 만들려고하는 완전히 새로운 오전!

답변

3

내가 대신 내 컨트롤러에서 이것을 사용했을 :

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

좋아 여전히 같은 오류가 발생, 그것을 시도. –

+1

이상한, 나를 위해 노력하고 있습니다. 이것을 시도하십시오 : @ resp.body 반환 – fmendez

3

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)