faraday 0.8.X 是否支持 typhoeus 客户端?
支持好;)
嗯我设置:
conn = Faraday.new(url, ssl: {verify: false}) 做 |builder|
builder.response :follow_redirects, :limit => 3, :standards_compatible => false, :cookie => :all
builder.adapter :typhoeus
结尾
请求已发送,但带有 net_http !
如何切换到 em_http 的typhoeus?
在 0.7.X typhoeus 运行良好。
我的堆栈跟踪:
https://gist.github.com/3743068
我设置了 typhoeus 或 em_http 适配器,但在日志中:
/home/johnbat26/.rvm/gems/ruby-1.9。 3-p194@webex/gems/faraday-0.8.4/lib/faraday/adapter/net_http.rb :54:in new'
/home/johnbat26/.rvm/gems/ruby-1.9.3-p194@webex/gems/faraday-0.8.4/lib/faraday/adapter/net_http.rb:54:in
create_request'
/home/johnbat26/.rvm/gems/ruby-1.9。 3-p194@webex/gems/faraday-0.8.4/lib/faraday/adapter/net_http.rb :74:in perform_request'
/home/johnbat26/.rvm/gems/ruby-1.9.3-p194@webex/gems/faraday-0.8.4/lib/faraday/adapter/net_http.rb:37:in
call'
服务器得到状态 503 的响应,但 net_http 得到错误:
错误
nil:NilClass 的未定义方法 `strip'
您正在像这样初始化法拉第连接:
conn = Faraday.new(url, ssl: {verify: false}) do |builder|
builder.response :follow_redirects, :limit => 3, :standards_compliant => false, :cookie => :all
builder.adapter :typhoeus
end
看起来没问题。 但是您是否使用相同的连接来发出请求? 您还没有向我们展示您使用 Faraday 执行请求的代码。 也许您正在使用两个不同的法拉第连接。
提示:尝试使用带有默认选项的 follow_redirects:
builder.response :follow_redirects
我在调试模式下看到对象 conn 使用适配器 typhoeus,但在日志中我看到 net_http?! 为什么?
我使用下一个代码:
require 'faraday'
require 'faraday_middleware'
require 'em-http'
require 'typhoeus'
module MediationHelper
# Parameters:
# url = args[:url]
# method = args[:method] || :post
# adapter = args[:adapter] || :net_http
# body = args[:body] || ""
# params = args[:params]
# authentication = args[:auth]
# headers = args[:headers] || {'Content-Type' => 'text/xml; charset=utf-8'}
# timeout = args[:timeout] || 60
# open_timeout = args[:open_timeout] || 60
#
def self.send_http_request(args)
url = args[:url]
method = args[:method] || :post
adapter = args[:adapter] || :net_http
params = args[:params]
body = args[:body] || ""
authentication = args[:auth]
headers = args[:headers] || {'Content-Type' => 'text/xml; charset=utf-8'}
timeout = args[:timeout] || 60
open_timeout = args[:open_timeout] || 60
conn = Faraday.new(url, ssl: {verify: false}) do |conn|
conn.response :follow_redirects
conn.adapter adapter
end
conn.basic_auth(args[:username], args[:password]) if authentication
response = conn.send(method) do |req|
req.headers.update headers
req.body = body
req.params = params if params
req.options = {
:timeout => timeout, # open/read timeout Integer in seconds
:open_timeout => open_timeout, # read timeout Integer in seconds
}
end
response
end
end
接下来我调用这个:
response_for_login_request = MediationHelper.send_http_request(:url => url,
:body => login_request_xml,
:adapter => :typhoeus
:headers => {'Content-Type' => "text/xml; charset=utf-8",
'Cookie' => my__cookie,
'SOAPAction' => '""'})
我设置:
conn.response :follow_redirects
并仍然得到错误:
例外
无方法错误
错误
nil:NilClass 的未定义方法 `strip'
/home/johnbat26/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:1435:in `block in initialize_http_header'
/home/johnbat26/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:1433:in `each'
/home/johnbat26/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:1433:in `initialize_http_header'
/home/johnbat26/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:1862:in `initialize'
/home/johnbat26/.rvm/gems/ruby-1.9.3-p194@webex/gems/faraday-0.8.4/lib/faraday/adapter/net_http.rb:54:in `new'
/home/johnbat26/.rvm/gems/ruby-1.9.3-p194@webex/gems/faraday-0.8.4/lib/faraday/adapter/net_http.rb:54:in `create_request'
/home/johnbat26/.rvm/gems/ruby-1.9.3-p194@webex/gems/faraday-0.8.4/lib/faraday/adapter/net_http.rb:74:in `perform_request'
/home/johnbat26/.rvm/gems/ruby-1.9.3-p194@webex/gems/faraday-0.8.4/lib/faraday/adapter/net_http.rb:37:in `call'
/home/johnbat26/.rvm/gems/ruby-1.9.3-p194@webex/gems/faraday_middleware-0.8.8/lib/faraday_middleware/response/follow_redirects.rb:76:in `perform_with_redirection'
/home/johnbat26/.rvm/gems/ruby-1.9.3-p194@webex/gems/faraday_middleware-0.8.8/lib/faraday_middleware/response/follow_redirects.rb:65:in `call'
/home/johnbat26/.rvm/gems/ruby-1.9.3-p194@webex/gems/faraday-0.8.4/lib/faraday/connection.rb:226:in `run_request'
/home/johnbat26/.rvm/gems/ruby-1.9.3-p194@webex/gems/faraday-0.8.4/lib/faraday/connection.rb:99:in `post'
/home/coding/projects/idea_projects/tsp-bwks-mediation/app/helpers/mediation_helper.rb:50:in `send_http_request'
/home/coding/projects/idea_projects/tsp-bwks-mediation/app/helpers/broadworks_helper.rb:81:in `authenticate'
/home/coding/projects/idea_projects/tsp-bwks-mediation/app/helpers/broadworks_helper.rb:121:in `init_broadworks_session'
/home/coding/projects/idea_projects/tsp-bwks-mediation/app/jobs/webex_requests_job.rb:24:in `block in perform'
/home/johnbat26/.rvm/gems/ruby-1.9.3-p194@webex/gems/nokogiri-1.5.5/lib/nokogiri/xml/node_set.rb:239:in `block in each'
/home/johnbat26/.rvm/gems/ruby-1.9.3-p194@webex/gems/nokogiri-1.5.5/lib/nokogiri/xml/node_set.rb:238:in `upto'
/home/johnbat26/.rvm/gems/ruby-1.9.3-p194@webex/gems/nokogiri-1.5.5/lib/nokogiri/xml/node_set.rb:238:in `each'
/home/coding/projects/idea_projects/tsp-bwks-mediation/app/jobs/webex_requests_job.rb:18:in `perform'
对不起。 是我的错误。
我做了 2 个请求,忘记了使用默认适配器的第二个请求。
对于 typhoeus,不会出现此错误。
但是如何处理 net_http 适配器并且出现错误: undefined method `strip' for nil:NilClass ?
抱歉我忘了回复这个。 您可能正在传递一个为零的标头值,而 net_http 试图将标头值作为字符串进行处理。
要解决此问题,请确保您传递的任何标头实际上都具有字符串值。
最有用的评论
抱歉我忘了回复这个。 您可能正在传递一个为零的标头值,而 net_http 试图将标头值作为字符串进行处理。
要解决此问题,请确保您传递的任何标头实际上都具有字符串值。