Search code examples
pythonruby-on-railsruby-on-rails-3net-http

Equivalent ruby code for python


What is the equivalent ruby code for the following python code. I surfed net and find net/http' is the correct method. But I am new to ruby, I am unable to use this method effectively. Can anybody help me to write this in python?

Python

url = "https://www.abcde.com/api/"
post_data = "method=a&rate=2&order=asc"
headers =  {"key"=>"55208252", "sign"=>"4589cab68921fb43ad53149ea625e29"}

url_request_object = urllib2.Request(url, post_data, headers)

response = urllib2.urlopen(url_request_object)

Solution

  • Signature of correspondent method is the same

    http = Net::HTTP.new('localhost', 3000)
    headers = {'Content-Type' =>  'application/json'}
    data = {:test => 'test'}
    
    resp, data = http.post(path, data.to_json, headers)