How do I post JSON via HTTP in Ruby after conversion from Python? -


i yield - i've tried hours crack nut can't figure out. i'm new ruby (and have no python background!) translate , post json data site requires user/pass, , response data.

this python code:

r = requests.post('https://keychain.oneid.com/validate/', json.dumps(data), auth=('username', 'password')) r.json() 

where data is:

{"some" => "data", "fun" => "times"} 

i'm trying replicate functionality of code in ruby use rails application, between figuring out how python requests.post() function operates , writing ruby code post , get, i've become totally lost.

i tried net::http i'm not clear if should putting username/pass in body or use basic_auth method -- basic_auth seems work inside net::http.get ... , net::http doesn't seem handle json, again, totally out lunch @ point.

any suggestions or appreciated!

use rest-client gem or use net::http.

ruby code(version 1.9.3):

require 'net/http' require 'json' require 'uri'  uri = uri('https://keychain.oneid.com/validate/') req = net::http::post.new uri.path # ruby 2.0: req = net::http::post.new uri req.basic_auth 'username', 'password' req.body = {:some => 'data', :fun => 'times'}.to_json  res = net::http.start(uri.host, uri.port, :use_ssl => true) |http|   http.verify_mode = openssl::ssl::verify_none   http.ssl_version = :sslv3   http.request req end  puts res.body # => {"errorcode": -99, "error": "invalid api credentials. please verify , try again"}  json = json.parse res.body puts json['errorcode'] # => -99 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -