2013-06-20 51 views
0

我的問題是 - 法拉第無法連接到網址。當我在瀏覽器中打開它http://localhost:3001/api/plans我得到了迴應。
但是當我試圖從鐵軌連接我得到響應狀態405和空的響應主體法拉第未連接到網址

url = 'http://localhost:3001/api/plans' 
    conn = Faraday.new(:url => url) do |faraday| 
    faraday.request :url_encoded    # form-encode POST params 
    faraday.response :logger     # log requests to STDOUT 
    faraday.adapter Faraday.default_adapter # make requests with Net::HTTP 
    end 
    resp = conn.post do |req| 
    req.headers['Content-Type'] = 'application/json' 
    req.body = data.to_json 
    end 
    logger.info '**********************************' 
    logger.debug "* Login params: #{data.inspect}" 
    logger.debug "* Url: #{url} " 
    logger.debug "* Response: #{resp.inspect} " 
    logger.debug "* Response body: #{resp.body.inspect} " 
    logger.info '**********************************' 

記錄儀的輸出爲:

********************************** 
* Login params: {:username=>"14122460670", :password=>"******", :remember_me=>"on"} 
* Url: http://localhost:3001/api/plans 
* Response: #<Faraday::Response:0x007f9f5c467528 @env={:method=>:post, :body=>"", :url=>#<URI::HTTP:0x007f9f480da450 URL:http://localhost:3001/v1/sessions>, :request_headers=>{"User-Agent"=>"Faraday v0.8.7", "Content-Type"=>"application/json"}, :parallel_manager=>nil, :request=>{:proxy=>nil}, :ssl=>{}, :status=>405, :response_headers=>{"allow"=>"OPTIONS, GET, HEAD", "content-type"=>"text/plain", "connection"=>"close", "server"=>"thin 1.5.1 codename Straight Razor"}, :response=>#<Faraday::Response:0x007f9f5c467528 ...>}, @on_complete_callbacks=[]> 
* Response body: "" 
********************************** 

爲什麼我可以利用法拉第怎麼沒有得到迴應我可以得到它嗎?

回答

1

錯誤代碼405意味着您正在使用的http方法不受支持。所以我認爲問題可能是在瀏覽器中使用GET方法訪問此網站,在法拉第它是POST。

配置法拉第發送GET請求。

resp = conn.get do |req| 
... 
+0

是的,最後服務器uset獲取奇怪任務的請求! – Elmor