title: Ruby send Json response to http post call
body: <p>I currently have Ruby code that takes a http post from the client.  It makes another http post, and I want to send a json response based on the response from the post.  How do I do this?  I am trying to use the answer from this post:  <a href="https://stackoverflow.com/questions/12385345/how-to-send-simple-json-response-in-rails">How to send simple json response in Rails?</a></p>
  
  <p>But it is giving me errors when I try to compile.</p>
  
  <pre><code>require 'sinatra'
  require 'rest-client'
  require 'sequel'
  require 'pg'
  require 'json/ext'
  
  require 'net/http'
  require 'json'
  
  require 'monza'
  require 'time'
  
  post '/receiptValidation' do
    # Find devices with the corresponding reg_tokens
    base64ReceiptDataString = params[:base64ReceiptDataString]
  
  
    uri = URI("https://sandbox.itunes.apple.com") 
      Net::HTTP.start(uri.host, uri.port, :use_ssl =&gt; uri.scheme == 
      'https') do |http|
        response = http.post('/verifyReceipt', params_json)
        parsedJson = JSON.parse(response.body)
  
        # ***** send JSON response here *******
        respond_to do |format|
        # ... other formats here ...
          format.jsonr do
            render :json =&gt; { 
              :status =&gt; :ok, 
              :message =&gt; "Success!",
              :html =&gt; "...insert html..."
            }.to_json
          end        
        end
    end
  end
  </code></pre>
  
  <p>This is the error I'm getting:  </p>
  
  <pre><code>2017-08-20 02:36:04 - NoMethodError - undefined method `respond_to' for #&lt;Sinatra::Application:0x007f0dcb04ba70&gt;
  2017-08-20T02:36:04.949145+00:00 app[web.1]: Did you mean?  respond_to?:
  2017-08-20T02:36:04.949146+00:00 app[web.1]:    firebasepushserver.rb:315:in `block in &lt;main&gt;'
  </code></pre>
  
  <p>I want to get the response in the following swift code: </p>
  
  <pre><code>        let task = URLSession.shared.dataTask(with: request) { data, response, error in
              guard let data = data, error == nil else {
                  // check for fundamental networking error
                  print("error=\(error)")
                  return
              }
  
              if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
                  // check for http errors
                  print("statusCode should be 200, but is \(httpStatus.statusCode)")
                  print("response = \(response)")
              }
  
              let responseString = String(data: data, encoding: .utf8)
              print("responseString = \(responseString)")
  
              print("done")
          }
          task.resume()
  </code></pre>