簡體   English   中英

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

[英]What is the equivalent ruby code for urllib2 in Python

我想傳遞網址,標題和query_params。 以下是python代碼。 我想用紅寶石做。 我上網沖浪,並且知道Net :: HTTP是執行此操作的正確方法。 但是此方法不接受查詢參數,並引發錯誤。 以下python代碼的ruby代碼等效於什么?

兩者都添加了文件下載鏈接

蟒蛇

紅寶石

我也想在Ruby中獲得與python同樣的結果。

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

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

蟒蛇

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}

這應該可以幫助您。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM