在GET请求中如何使用Rails中的httparty的区分大小写的标头?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (118)

当我使用httparty发出GET请求时,我当前收到错误。我使用时curl。错误如下:

\“Authdate \”:\“1531403501 \”}“},{”error_code“:”external_auth_error“,”error_message“:”日期标题丢失或时间戳超出界限“}}}

curl -X GET -H "AuthDate: 1531403501"

但是,正如你所看到的,请求会更改AuthDateAuthdate导致错误。以下是我的调用方式:

require 'openssl'
require 'base64'

module SeamlessGov
  class Form
    include HTTParty
    attr_accessor :form_id
    base_uri "https://nycopp.seamlessdocs.com/api"

    def initialize(id)
      @api_key = ENV['SEAMLESS_GOV_API_KEY']
      @signature = generate_signature
      @form_id = id
      @timestamp = Time.now.to_i
    end

    def relative_uri
      "/form/#{@form_id}/elements"
    end

    def create_form
      self.class.get(relative_uri, headers: generate_headers)
    end

    private

    def generate_signature
      OpenSSL::HMAC.hexdigest('sha256', ENV['SEAMLESS_GOV_SECRET'], "GET+#{relative_uri}+#{@timestamp}")
    end

    def generate_headers
      {
        "Authorization"  => "HMAC-SHA256 api_key='#{@api_key}' signature='#{@signature}'",
        "AuthDate" => @timestamp
      }
    end
  end
end

有任何解决方法吗?

提问于
用户回答回答于

我创建了文件lib/net_http.rb

require 'net/http'

class Net::HTTP::ImmutableHeaderKey
  attr_reader :key

  def initialize(key)
    @key = key
  end

  def downcase
    self
  end

  def capitalize
    self
  end

  def split(*)
    [self]
  end

  def hash
    key.hash
  end

  def eql?(other)
    key.eql? other.key.eql?
  end

  def to_s
    def self.to_s
      key
    end
    self
  end
end

然后在标题中

def generate_headers
      {
        "Authorization"  => "HMAC-SHA256 api_key='#{@api_key}' signature='#{@timestamp}'",
         Net::HTTP::ImmutableHeaderKey.new('AuthDate') => "#{@timestamp}"
      }
end
用户回答回答于

扫码关注云+社区

领取腾讯云代金券