【问题标题】:Faraday not connecting to url法拉第没有连接到网址
【发布时间】:2013-06-20 07:05:24
【问题描述】:

我的问题是 - Faraday 无法连接到 url。当我在浏览器 http://localhost:3001/api/plans 中打开它时,我得到了响应。
但是当我尝试从 rails 连接时,我得到响应状态 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: "" 
**********************************  

为什么我无法使用 Faraday 获得响应,我如何获得响应?

【问题讨论】:

    标签: ruby ruby-on-rails-3 http response faraday


    【解决方案1】:

    错误代码 405 表示您使用的 http 方法不受支持。所以我认为问题可能是在浏览器中您使用 GET 方法访问该站点,而在法拉第它是 POST。

    配置 Faraday 以发送 GET 请求。

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

    【讨论】:

    • 是的,最终服务器使用了获取奇怪任务的请求!
    猜你喜欢
    • 1970-01-01
    • 2018-10-26
    • 1970-01-01
    • 1970-01-01
    • 2014-10-10
    • 2012-04-01
    • 2019-01-15
    • 2015-05-31
    • 1970-01-01
    相关资源
    最近更新 更多