ruby - How to post json data with Rest API -


require 'net/http' require 'uri' postdata = net::http.post_form(uri.parse('http://localhost/restapi/index.php/api/posts'),                             {'id'=>9,'firstname'=>"test","lastname"=>"test"}) puts postdata.body 

how can send data in json form?

@tosend = {"id" =>5,"firstname" => "anurag","lastname" => "arya"} 

i tried did not work:

@tosend.to_json 

example:

require 'rubygems' require 'net/http' require 'uri' require 'json'  url = "http://localhost/restapi/index.php/api/posts" uri = uri.parse(url) data = {"id"=>11,                                                         "firstname"=>"pwd","lastname"=>"last"}       headers = {'content-type' =>'application/json',                                     'accept-encoding'=> "gzip,deflate",                      'accept' => "application/json"}  http = net::http.new(uri.host,uri.port)   # creates http object #http.use_ssl = true                                          # when using https #http.verify_mode = openssl::ssl::verify_none response = http.post(uri.path,data.to_json,headers) puts response.code puts response.body 

postdata=net::http.post_form(uri.parse('http://localhost/oecprashant/yiiindex.php/api/rubyrest'),                             {'data'=>jsondata}) 

Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -