Morris
Morris

Reputation: 520

How to send a json to rails app through HTTP

I have a rails app which has a model called account. I have an mobile app that constroys a json with account's number and a debit value that does not exist in my model. I have no idea how to receive this in my server side application in rails. This json is will be sent through HTTP put method. Can anybody help me, please?

Thanks in advance!

Upvotes: 0

Views: 35

Answers (1)

Tucker
Tucker

Reputation: 659

In your application_controller.rb modify:

  protect_from_forgery with: :exception

to

    protect_from_forgery with: :null_session, only: Proc.new { |c| c.request.format == 'application/json' }

This will allow your rails application to accept json request. Then you can simply send a json request to whatever action you want, via a http put/post method.

Your receiving action will look like this:

def some_action
  # do something with the parameters
  respond_to do |format|
    format.html { redirect_to somewhere_path }
    format.json { render json: { success:  :true }, status: :ok }
  end
end

Upvotes: 1

Related Questions