在 HTTparty 中传递标头和查询参数 [英] Passing headers and query params in HTTparty

查看:9
本文介绍了在 HTTparty 中传递标头和查询参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to pass query params and headers in post method using HTTparty. 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 )

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

But it throws the following error. How to handle query string params and headers with 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
)

由于 net/http/header.rb:172.重要信息是 undefined method 'split' for :key:Symbol (NoMethodError)

It was probably only the headers that 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)

irb 中的符号错误:

Symbol error in 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>'

这篇关于在 HTTparty 中传递标头和查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆