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/6/EmptyTag/128.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 Http Post参数_Ruby_Http_Post_Parameters_Uri - Fatal编程技术网

Ruby Http Post参数

Ruby Http Post参数,ruby,http,post,parameters,uri,Ruby,Http,Post,Parameters,Uri,如何将post参数添加到我现在拥有的内容中: @toSend = { "nonce" => Time.now.to_i, "command" => "returnCompleteBalances" }.to_json uri = URI.parse("https://poloniex.com/tradingApi") https = Net::HTTP.new(uri.host,uri.port) ht

如何将post参数添加到我现在拥有的内容中:

@toSend = {
        "nonce" => Time.now.to_i,
        "command" => "returnCompleteBalances"
    }.to_json    

uri = URI.parse("https://poloniex.com/tradingApi")
        https = Net::HTTP.new(uri.host,uri.port)
        https.use_ssl = true
        https.verify_mode = OpenSSL::SSL::VERIFY_NONE
        req = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' =>'application/json'})
        req.set_form_data({"nonce" => Time.now.to_i, "command" => "returnCompleteBalances"})
        req['Key'] = '******-N4WZI2OG-******-10RX5JYR'
        req['Sign'] = 'secret_key'

        req.body = "[ #{@toSend} ]"
        res = https.request(req)
        puts "Response #{res.code} #{res.message}: #{res.body}"
以下是我要发送的参数:

"nonce" => Time.now.to_i,
"command" => "returnCompleteBalances"

谢谢。

您似乎正在尝试使用Poloniex的交易API。如果这是您的首要目标,您可能希望考虑使用一个库来处理琐碎的细节。例如:

如果您的主要目标不是简单地使用API,而是将其作为一种学习体验,那么以下是几点提示:

  • API文档表明API接受表单编码的POST数据(不是JSON),但用JSON响应
  • key参数(“key”)类似于您的用户id。它允许Poloniex了解谁试图针对API发出请求
  • 签名参数(“Sign”)是根据密钥内容和消息内容(编码表单数据)生成的HMAC。这会产生一种只有您和Poloniex才能复制的指纹信息,从而在一定程度上保证您的请求来自密钥所有者。当然,这假设您的密钥确实只有您知道
我不使用Poloniex exchange,也无法测试此代码,但我相信这已接近您试图实现的目标:

require 'net/http'
require 'openssl'

secret = 'your-secret-key'
api_key = 'your-api-key'
uri = URI('https://poloniex.com/tradingApi')

http = Net::HTTP.new(uri.host)
request = Net::HTTP::Post.new(uri.request_uri)
form_data = URI.encode_www_form({:command => 'returnBalances', :nonce => Time.now.to_i * 1000 })
request.body = form_data
request.add_field('Key', api_key)
request.add_field('Sign', OpenSSL::HMAC.hexdigest( 'sha512', secret, form_data))

res = http.request(request)
puts res.body