title: How to post json data with Rest API
body: <pre><code>require 'net/http'
  require 'uri'
  postData = Net::HTTP.post_form(URI.parse('http://localhost/restapi/index.php/api/posts'), 
                             {'id'=&gt;9,'firstname'=&gt;"test","lastname"=&gt;"test"})
  puts postData.body
  </code></pre>
  
  <p>How can I send data in JSON form?</p>
  
  <pre><code>@toSend = {"id" =&gt;5,"firstname" =&gt; "anurag","lastname" =&gt; "arya"}
  </code></pre>
  
  <p>I also tried this but it did not work:</p>
  
  <pre><code>@toSend.to_json
  </code></pre>
  
  <p>Example:</p>
  
  <pre><code>require 'rubygems'
  require 'net/http'
  require 'uri'
  require 'json'
  
  url = "http://localhost/restapi/index.php/api/posts"
  uri = URI.parse(url)
  data = {"id"=&gt;11,                                          
                "firstname"=&gt;"PWD","lastname"=&gt;"last"}     
  
  headers = {'Content-Type' =&gt;'application/json',               
                       'Accept-Encoding'=&gt; "gzip,deflate",
                       'Accept' =&gt; "application/json"}
  
  http = Net::HTTP.new(uri.host,uri.port)   # Creates a 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
  </code></pre>