简体   繁体   English

在HTTparty中传递标题和查询参数

[英]Passing headers and query params in HTTparty

How to pass query params and headers in post method using HTTparty. 如何使用HTTparty在post方法中传递查询参数和头文件。 I am doing as follows But it throws 我正在做如下但它抛出

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'

Use Strings for your hash keys instead of 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 headers that were causing a problem due to the error occurring in net/http/header.rb:172 . 由于net/http/header.rb:172发生错误,可能只有导致问题的headers The important info being undefined method 'split' for :key:Symbol (NoMethodError) 重要信息是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>'

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM