【问题标题】:How to use adapter to prevent Faraday from changing capitalize headers如何使用适配器防止法拉第更改大写标题
【发布时间】:2019-10-18 22:13:28
【问题描述】:

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

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-Signature 和API_REQUEST_TIME 更改为Api-Request-Time。

我想防止这种情况发生。有人建议使用赞助人:

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

但这不起作用。出现以下错误:

/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>'

谢谢。

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

问题减少了:

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

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

puts conn.headers

【问题讨论】:

    标签: ruby adapter faraday


    【解决方案1】:

    出现此错误是因为 Faraday 默认使用 Net::HTTP 和 Net::HTTP changes the case of your header keys。您可以在this related question 阅读有关此问题的更多信息。

    您可以使用https://lostisland.github.io/faraday/adapters/ 中列出的其他可用适配器之一解决此问题:

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

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

    更新

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

    # 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"}
    

    对比:

    # stringified
    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"}
    

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

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

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

    【讨论】:

    • 太奇怪了,它们都不起作用。你认为它可能是我正在使用的 Ruby 版本吗? (2.5.3 随 rvm 安装)。
    • 减少你的例子,这样你就不用类了,只是一个简单的Faraday.new(...) do |f|块,你的所有数据都硬编码到里面,然后再试一次。
    • 在 Ruby 中非常简单。你想要一个符号化的字符串作为键吗?使用冒号。 ({ foo: 'bar' }) 你想使用任何对象作为键而不改变它吗?使用哈希火箭。 ({ 1 =&gt; 'bar' } 或{ [] =&gt; 'bar' } 或{ Faraday.new =&gt; 'bar' } 或{ 'foo' =&gt; 'bar' } 甚至{ :foo =&gt; 'bar' })
    【解决方案2】:

    解决方案是使用火箭分配而不是冒号。像这样:

    headers = {
          'Content-Type': 'application/json',
          'x-api-key': '0000'
        }
    ...
    conn.headers
    => {"Content-type"=>"application/json", "X-api-key"=>"0000", "User-Agent"=>"Faraday v0.17.3"}
    

    VS

    headers = {
          'Content-Type' => 'application/json',
          'x-api-key' => '0000'
        }
    conn.headers
    => {"Content-Type"=>"application/json", "x-api-key"=>"0000", "User-Agent"=>"Faraday v0.17.3"} 
    

    请注意 x 是如何用冒号大写的,而不是如何不使用火箭。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-25
      • 2018-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-29
      相关资源
      最近更新 更多