我的问题是-法拉第无法连接到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 '**********************************'logger的输出为:
**********************************
* 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获得回复,以及如何获得回复?
发布于 2013-06-20 16:53:39
错误码405表示不支持您使用的http方法。所以我认为问题可能是在浏览器中使用GET方法访问这个站点,而在Faraday中它是POST。
配置Faraday以发送GET请求。
resp = conn.get do |req|
...https://stackoverflow.com/questions/17207292
复制相似问题