Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 使用rails中的HttpParty在get请求中使用区分大小写的头_Ruby_Ruby On Rails 5_Httparty_Net Http - Fatal编程技术网

Ruby 使用rails中的HttpParty在get请求中使用区分大小写的头

Ruby 使用rails中的HttpParty在get请求中使用区分大小写的头,ruby,ruby-on-rails-5,httparty,net-http,Ruby,Ruby On Rails 5,Httparty,Net Http,当我使用HttpParty发出GET请求时,我当前收到一个错误。当我使用curl时,调用会起作用。错误如下: \“Authdate\:\“1531403501\”},{“错误代码”: “外部身份验证错误”、“错误消息”:“缺少日期头或 时间戳超出范围“}]} 当我通过curl发出请求时,这是我使用的标题 curl-X GET-H“AuthDate:1531403501” 但是,如您所见,请求从AuthDate更改为AuthDate导致错误。下面是我如何进行调用: require 'openssl

当我使用HttpParty发出GET请求时,我当前收到一个错误。当我使用
curl
时,调用会起作用。错误如下:

\“Authdate\:\“1531403501\”},{“错误代码”: “外部身份验证错误”、“错误消息”:“缺少日期头或 时间戳超出范围“}]}

当我通过
curl
发出请求时,这是我使用的标题

curl-X GET-H“AuthDate:1531403501”

但是,如您所见,请求从
AuthDate
更改为
AuthDate
导致错误。下面是我如何进行调用:

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

任何解决方法?

根据规范,标头都不区分大小写,因此您访问的服务器似乎是错误的

看看,在哪个HttpParty上实现

与原始散列访问不同,HTTPHeader通过不区分大小写的键提供访问

它看起来像是一个类,为统一性设置了标题键


您可能需要查看一个不同的网络库,它不依赖于
net/http

我创建了文件
lib/net\u 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