0

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: How to send simple json response in Rails?

But it is giving me errors when I try to compile.

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 => 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 => { 
            :status => :ok, 
            :message => "Success!",
            :html => "...insert html..."
          }.to_json
        end        
      end
  end
end

This is the error I'm getting:

2017-08-20 02:36:04 - NoMethodError - undefined method `respond_to' for #<Sinatra::Application:0x007f0dcb04ba70>
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 <main>'

I want to get the response in the following swift 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()
6
  • What is the error message? Commented Aug 20, 2017 at 1:12
  • 2017-08-20 02:36:04 - NoMethodError - undefined method respond_to' for #<Sinatra::Application:0x007f0dcb04ba70> 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 <main>' Commented Aug 20, 2017 at 2:36
  • It seems your app is a Sinatra application rather than rails. Consider edit the tag and removes ruby-on-rails tag in the question. Commented Aug 20, 2017 at 2:39
  • yes, it is a sinatra application. I didn't know that was different from rails. so I need to find how to do it with sinatra? Commented Aug 20, 2017 at 2:40
  • They are different web framework although the are both written in Ruby. Commented Aug 20, 2017 at 2:41

1 Answer 1

1

You may change

respond_to do |format|
  # ... other formats here ...
  format.jsonr do
    render :json => {
        :status => :ok,
        :message => "Success!",
        :html => "...insert html..."
    }.to_json
  end
end

to

  content_type :json
  {
      :status => :ok,
      :message => "Success!",
      :html => "...insert html..."
  }.to_json

It uses content_type, you can find the doc here.

Sign up to request clarification or add additional context in comments.

8 Comments

I tried replacing the code block with the one you gave, but the response is blank. the docs say that this code fills the response, but what is the command to actually send the response?
@mjpablo23 This code will definitely work as long as you add the code in the post block of sinatra. Because I test it in my computer.
ah i see. I have it in the post block. I keep getting this as a response in swift: response = Optional(<NSHTTPURLResponse: 0x170237020> { URL: appname.herokuapp.com/receiptvalidation } { status code: 404, headers { Connection = "keep-alive";
this is what it prints on heroku: [20/Aug/2017:05:29:27 +0000] "POST /receiptvalidation HTTP/1.1" 404 18 0.0024
oh I fixed the 404 problem. but the response is still blank right now.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.