本文介绍了如何使用适配器来防止Faraday更改大写标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Faraday创建将与API交互的SDK,并且我需要发送两个标头API_SIGNATURE和API_REQUEST_TIME,所以这就是我创建的:

I'm using Faraday to create an SDK that will interact with an API, and I need to send two headers API_SIGNATURE and API_REQUEST_TIME, so that's what I've created:

class APIClient
  def initialize(api_key)
    @api_key = api_key
  end

  def get_users
    request.post('/users')
  end

  private

  def request
    Faraday.new(@@BASE_API_URL, headers: headers)
  end

  def headers
    timestamp = Time.now.to_i.to_s
    return {
      'API_SIGNATURE': Digest::MD5.hexdigest(@api_key + timestamp),
      'API_REQUEST_TIME': timestamp
    }
  end
end

由于某种原因,法拉第将 API_SIGNATURE 更改为 Api签名 API_REQUEST_TIME Api请求时间

And for some reason Faraday is changing API_SIGNATURE to Api-Signature and API_REQUEST_TIME to Api-Request-Time.

我想防止从发生。有人建议使用赞助人:

I would like to prevent that from happening. Someone suggested to use patron:

  def request
    Faraday.new(@@BASE_API_URL, headers: headers) do |faraday|
      faraday.adapter :patron
    end
  end

但是不起作用。引发以下错误:

But that doesn't work. The following error is raised:

/Users/me/.rvm/gems/ruby-2.4.9/gems/patron-0.13.3/lib/patron/session.rb:330:in `handle_request': Operation timed out after 1004 milliseconds with 0 out of 0 bytes received (Faraday::TimeoutError)
        from /Users/me/.rvm/gems/ruby-2.4.9/gems/patron-0.13.3/lib/patron/session.rb:330:in `request'
        from /Users/me/.rvm/gems/ruby-2.4.9/gems/faraday-0.17.0/lib/faraday/adapter/patron.rb:29:in `call'
        from /Users/me/.rvm/gems/ruby-2.4.9/gems/faraday-0.17.0/lib/faraday/rack_builder.rb:143:in `build_response'
        from /Users/me/.rvm/gems/ruby-2.4.9/gems/faraday-0.17.0/lib/faraday/connection.rb:387:in `run_request'
        from /Users/me/.rvm/gems/ruby-2.4.9/gems/faraday-0.17.0/lib/faraday/connection.rb:175:in `post'
        from api.rb:32:in `analyze_text'
        from api.rb:38:in `full_analysis'
        from api.rb:65:in `<main>'

谢谢u。

=======更新==========

======= Update ==========

问题减少了:

headers = { API_SIGNATURE: '', API_REQUEST_TIME: '' }

conn = Faraday.new('https://api.test.io', headers: headers) do |f|
  f.adapter :patron
end

puts conn.headers


推荐答案

发生此错误是因为Faraday默认使用Net :: HTTP,并且。您可以在中了解有关此问题的更多信息。

This error occurs because Faraday uses Net::HTTP by default and Net::HTTP changes the case of your header keys. You can read more about this issue at this related question.

您可以使用:





  • https://lostisland.github.io/faraday/adapters/excon
  • https://lostisland.github.io/faraday/adapters/patron
  • https://lostisland.github.io/faraday/adapters/httpclient

或需要其他gem的外部适配器:

Or external adapters that require another gem:




  • https://github.com/typhoeus/typhoeus
  • https://github.com/lostisland/faraday-http

您对Patron的特定实现看起来是正确的,因此请尝试使用其他适配器之一,看看您是否有更好的运气。

Your specific implementation of Patron looks correct, so try using one of the other adapters to see if you have any better luck.

更新

我加载了您的更新示例并自己进行了测试。解决方案是使用字符串化的密钥。您正在使用符号键。

I loaded your updated example and tested it myself. The solution is to use stringified keys. You're using symbolized keys.

# symbolized
headers = { API_SIGNATURE: '', API_REQUEST_TIME: '' }
=> {
       :API_SIGNATURE => "",
    :API_REQUEST_TIME => ""
}

这将返回:

puts conn.headers
{"Api-Signature"=>"", "Api-Request-Time"=>"", "User-Agent"=>"Faraday v0.17.0"}

vs:

# stringified
headers = { 'API_SIGNATURE' => '', 'API_REQUEST_TIME' => '' }
=> {
       "API_SIGNATURE" => "",
    "API_REQUEST_TIME" => ""
}

现在您将获得正确的值:

Now you get the proper values:

puts conn.headers
{"API_SIGNATURE"=>"", "API_REQUEST_TIME"=>"", "User-Agent"=>"Faraday v0.17.0"}

乍一看,您出现的原始示例 字符串化但不是(这就是为什么我没有抓住它):

At a glance, your original example appears stringified but is not (which is why I didn't catch it):

{
  'API_SIGNATURE': '',
  'API_REQUEST_TIME': ''
}
=> {
       :API_SIGNATURE => "",
    :API_REQUEST_TIME => ""
}

在Ruby中使用冒号作为哈希键会自动使其成为符号,即使它是带引号的字符串。您需要对哈希密钥使用哈希火箭语法。

Using a colon for your hash key in Ruby automatically makes it a symbol, even if it's a quoted string. You need to use the hash rocket syntax for stringified keys.

这篇关于如何使用适配器来防止Faraday更改大写标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 14:10