title: Ruby: Receive and print JSON object
body: <p>I want to receive a JSON object using Ruby. I've written this:</p>
  
  <pre><code>require 'sinatra'
  require 'json'
  
  post '/' do
    push = JSON.parse(params[:payload])
    "I got some JSON: #{push.inspect}"
  end
  </code></pre>
  
  <p>And I'm sending:</p>
  
  <pre><code>var options = {
                    host: 'localhost',
                    port: 4567,
                    path: '/',
                    method: 'POST'
                  };
  
  var myFirstJSON = { "payload" : { "headers" : 
                                              [{ "from" : from,
                                                  "to"  : to,
                                                  "subject" : subject }],
                                  "body"    : 
                                              [{ "primeira_parte" : primeira_parte,
                                                  "segunda_parte" : segunda_parte,
                                                  "terceira_parte": terceira_parte }]
                                  }};
              req.write(JSON.stringify(myFirstJSON));
  </code></pre>
  
  <p>However, I'm getting this error:</p>
  
  <pre><code>TypeError - can't convert nil into String:
  
  
  {"{\"payload\":{\"headers\":"=&gt;{"{\"from\":\"test@test.com\",\"to\":\"test@test.com\",\"subject\":\"Testing\"}"=&gt;{",\"body\":"=&gt;{"{\"primeira_parte\":\"The following message to &lt;test@test.com&gt; was undeliverable.\\r\\nThe reason for the problem:\\r\\n5.1.0 - Unknown address error 553-'sorry, this recipient is not in my valid"=&gt;"\\r\\nrcptto list (#5.7.1)'\",\"segunda_parte\":\"Final-Recipient: rfc822"}}}, "test@test.com\\r\\nAction: failed\\r\\nStatus: 5.0.0 (permanent failure)\\r\\nRemote-MTA: dns"=&gt;nil, "216.75.35.163"=&gt;{"\\r\\nDiagnostic-Code: smtp"=&gt;nil}, "5.1.0 - Unknown address error 553-'sorry, this recipient is not in my validrcptto list (#5.7.1)' (delivery attempts: 0)\",\"terceira_parte\":\"Received: from unknown (HELO aws-bacon-delivery-svc-iad-1020.vdc.amazon.com) ("=&gt;{"10.144.21.123"=&gt;{")\\r\\n  by na-mm-outgoing-6102-bacon.iad6.amazon.com with ESMTP"=&gt;nil}}, "16 Apr 2011 14:11:15  0000\\r\\nReturn-Path: 0000012f5eb1cab3-09564031-57ef-4136-8cd7-9f368c5acd7d-000000@email-bounces.amazonses.com\\r\\nDate: Sat, 16 Apr 2011 14:23:20  0000\\r\\nFrom: tiago@tiagop.org\\r\\nTo: test@test.com\\r\\nMessage-ID: &lt;0000012f5eb1cab3-09564031-57ef-4136-8cd7-9f368c5acd7d-000000@email.amazonses.com&gt;\\r\\nSubject: Testing\\r\\nMime-Version: 1.0\\r\\nContent-Type: text/plain"=&gt;nil, "\\r\\n charset"=&gt;"UTF-8\\r\\nContent-Transfer-Encoding: 7bit\\r\\nX-AWS-Outgoing: 199.255.192.79\\r\\n\\r\\n&lt;html&gt;&lt;body&gt;&lt;p&gt; Helllooo &lt;/p&gt;&lt;/body&gt;&lt;/html&gt;\\r\\n\\r\\n\"}]}}"}
  </code></pre>