Ruby-on-rails 在 HTTparty 中传递标头和查询参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24691483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:47:23  来源:igfitidea点击:

Passing headers and query params in HTTparty

ruby-on-railsrubyhttp-headershttparty

提问by Sam

How to pass query params and headers in post method using HTTparty. I am doing as follows But it throws

如何使用 HTTparty 在 post 方法中传递查询参数和标题。我正在做如下但它抛出

query = {:method => "neworder", :nonce => 1404996028, :order_type => "buy", :quantity=>1,:rate=>1}
headers = {:key=> "87819747209090199871234", :sign=> "0a3888ac7f8e411ad73a0a503c55db70a291rsf34bfb9f9a47147d5200882674f717f6ede475669f3453"}

HTTParty.post("https://www.acb.com/api/v2/market/LTC_BTC/", :query => query, :headers => headers )

But it throws the following error. How to handle query string params and headers with HTTparty.

但它会引发以下错误。如何使用 HTTparty 处理查询字符串参数和标头。

/home/user/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/net/http/header.rb:172:in `capitalize': undefined method `split' for :key:Symbol (NoMethodError)
from /home/user/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/net/http/header.rb:165:in `block in each_capitalized'
from /home/user/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/net/http/header.rb:164:in `each'
from /home/user/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/net/http/header.rb:164:in `each_capitalized'

回答by Matt

Use Stringsfor your hash keys instead of Symbols.

使用Strings作为哈希键而不是Symbols

query = { 
  "method"     => "neworder",
  "nonce"      => 1404996028,
  "order_type" => "buy",
  "quantity"   => 1,
  "rate"       => 1
}
headers = { 
  "key"  => "8781974720909019987",
  "sign" => "0a3888ac7f8e411ad73a0a503c55db70a291bfb9f9a47147d5200882674f717f6ede475669f3453" 
}

HTTParty.post(
  "https://www.acb.com/api/v2/market/LTC_BTC/", 
  :query => query,
  :headers => headers
)

It was probably only the headersthat were causing a problem due to the error occurring in net/http/header.rb:172. The important info being undefined method 'split' for :key:Symbol (NoMethodError)

这可能是只headers说是造成问题,因为中出现的错误net/http/header.rb:172。重要的信息是undefined method 'split' for :key:Symbol (NoMethodError)

Symbol error in irb:

中的符号错误irb

irb(main):002:0> "Something".split
=> ["Something"]

irb(main):003:0> :Something.split
NoMethodError: undefined method `split' for :Something:Symbol
        from (irb):3
        from /usr/bin/irb:12:in `<main>'

回答by Winters

It's been a bit old question, but we had the same issue recently, so I try to attach my solutions:

这是一个有点老的问题,但我们最近遇到了同样的问题,所以我尝试附上我的解决方案:

1) The answer above is working:

1)上面的答案是有效的:

  "headers": {
      "Authorization" => "Bearer #{token}"
    }

2) Alternatively, the other solution is:

2)或者,另一种解决方案是:

  headers: {
      Authorization: "Bearer #{token}"
    }