Ruby send Json response to http post call -


i have ruby code takes http post client. makes http post, , want send json response based on response post. how do this? trying use answer post: how send simple json response in rails?

but giving me errors when try compile.

require 'sinatra' require 'rest-client' require 'sequel' require 'pg' require 'json/ext'  require 'net/http' require 'json'  require 'monza' require 'time'  post '/receiptvalidation'   # find devices 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') |http|       response = http.post('/verifyreceipt', params_json)       parsedjson = json.parse(response.body)        # ***** send json response here *******       respond_to |format|       # ... other formats here ...         format.jsonr           render :json => {              :status => :ok,              :message => "success!",             :html => "...insert html..."           }.to_json         end               end   end end 

this error i'm getting:

2017-08-20 02:36:04 - nomethoderror - undefined method `respond_to' #<sinatra::application:0x007f0dcb04ba70> 2017-08-20t02:36:04.949145+00:00 app[web.1]: did mean?  respond_to?: 2017-08-20t02:36:04.949146+00:00 app[web.1]:    firebasepushserver.rb:315:in `block in <main>' 

i want response in following swift code:

        let task = urlsession.shared.datatask(with: request) { data, response, error in             guard let data = data, error == nil else {                 // check fundamental networking error                 print("error=\(error)")                 return             }              if let httpstatus = response as? httpurlresponse, httpstatus.statuscode != 200 {                 // check http errors                 print("statuscode should 200, \(httpstatus.statuscode)")                 print("response = \(response)")             }              let responsestring = string(data: data, encoding: .utf8)             print("responsestring = \(responsestring)")              print("done")         }         task.resume() 

you may change

respond_to |format|   # ... other formats here ...   format.jsonr     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, can find doc here.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -