什么是Python中urllib2的等效ruby代码

时间:2014-07-10 13:43:17

标签: python ruby-on-rails ruby ruby-on-rails-4 ruby-on-rails-3.2

我想传递url,header和query_params。以下是python代码。我想用红宝石做。我上网了,知道Net :: HTTP是这样做的正确方法。但是这种方法不接受查询参数并抛出错误。以下python代码的ruby代码的等价物是什么?

添加了文件下载链接

python

ruby

我希望在ruby中也有与python中相同的结果。

nonce=1404996712857&method=neworder&rate=1&order_type=buy&quantity=1  //post_data(query params)

headers = {'key': '1234567894444444', 'sign': 'jhjhgyyqw7y890190800iihuhugdhugabnmKOP'}

的Python

url_request_object = urllib2.Request("%s/%s" % (BASE_API_URL,url_suffix),
                                         post_data,
                                         headers)
response = urllib2.urlopen(url_request_object)

红宝石

response = Net::HTTP.post_form(uri, :body => post_data,:header => headers )
puts response.body

错误:

{"status": false, "message": "method required", "systime": 1404996498.0}

1 个答案:

答案 0 :(得分:0)

这应该会帮助你。

require 'net/http'

html_request = Net::HTTP.new('site.com', 80)
path = '/path/where/form/is'
headers = {'Host' => 'site.com', 'Connection' => 'keep-alive', 'Key' => '1234567894444444', 'sign' => 'jhjhgyyqw7y890190800iihuhugdhugabnmKOP'}
post_data = 'nonce=1404996712857&method=neworder&rate=1&order_type=buy&quantity=1'

response = html_request.post(path, post_data, headers)
response_string = response.body.to_s