Pertanyaan Melewati header dan params kueri di HTTparty


Cara melewatkan params kueri dan header dalam metode posting menggunakan HTTparty. Saya melakukan sebagai berikut Tapi itu melempar

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 )

Tapi itu melempar kesalahan berikut. Bagaimana menangani params string kueri dan header dengan 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'

34
2017-07-11 06:37


asal


Jawaban:


Menggunakan String untuk kunci hash Anda, bukan Simbol.

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
)

Itu mungkin hanya itu headers yang menyebabkan masalah karena kesalahan yang terjadi di net/http/header.rb:172. Info penting sedang undefined method 'split' for :key:Symbol (NoMethodError)

Kesalahan simbol di 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>'

71
2017-07-11 07:06



Sudah menjadi pertanyaan lama, tetapi kami memiliki masalah yang sama baru-baru ini, jadi saya mencoba melampirkan solusi saya:

1) Jawaban di atas berfungsi:

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

2) Alternatif lain, solusinya adalah:

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

5
2017-11-07 03:56