Ruby Http Post参数 [英] Ruby Http Post Parameters

查看:170
本文介绍了Ruby Http Post参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I add post parameters to what I have right now:

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

这些是我想发送的参数:

These are the params I want to send:

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

谢谢。

推荐答案

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

It appears that you're trying to use Poloniex's trading API. If this is your primary goal, you might wish to consider using a library to handle the nitty-gritty details. For example:

https://github.com/Lowest0ne / poloniex

如果您的主要目标不仅仅是使用API​​,而是将其用作学习体验,请参考以下几点:

If your primary goal is not simply to use the API, but to use this as a learning experience, here are a few pointers:


  • API文档表明API接受表单编码的POST数据(不是JSON),但是使用JSON进行响应。

  • 关键参数(Key)就像您的用户ID一样。它允许Poloniex了解谁正在尝试对API发出请求。

  • 签名参数(Sign)是根据您的密钥内容和内容生成的HMAC。你的消息(编码的表格数据)。这会生成一种指纹,只有您和Poloniex才能获得重现的信息,从而可以保证您的请求来自密钥的所有者。当然,这假设您的密钥确实只有您知道。

我不使用Poloniex交换而无法测试这段代码,但我相信这接近你想要完成的事情:

I don't use the Poloniex exchange and cannot test this code, but I believe this is close to what you're attempting to accomplish:

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

这篇关于Ruby Http Post参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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