For some reason, I get an InvalidAuthenticityToken when sending mail requests to my application when using json or xml. I understand that rails should require an authentication token only for html or js requests, and therefore I should not encounter this error. The only solution I have found so far is to disable_from_forgery protection for any actions that I would like to get through the API, but this is not ideal for obvious reasons. Thoughts?
def create respond_to do |format| format.html format.json{ render :json => Object.create(:user => @current_user, :foo => params[:foo], :bar => params[:bar]) } format.xml{ render :xml => Object.create(:user => @current_user, :foo => params[:foo], :bar => params[:bar]) } end end
and this is what I get in the logs whenever I submit an action request:
Processing FooController#create to json (for 127.0.0.1 at 2009-08-07 11:52:33) [POST] Parameters: {"foo"=>"1", "api_key"=>"44a895ca30e95a3206f961fcd56011d364dff78e", "bar"=>"202"} ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken): thin (1.2.2) lib/thin/connection.rb:76:in `pre_process' thin (1.2.2) lib/thin/connection.rb:74:in `catch' thin (1.2.2) lib/thin/connection.rb:74:in `pre_process' thin (1.2.2) lib/thin/connection.rb:57:in `process' thin (1.2.2) lib/thin/connection.rb:42:in `receive_data' eventmachine (0.12.8) lib/eventmachine.rb:242:in `run_machine' eventmachine (0.12.8) lib/eventmachine.rb:242:in `run' thin (1.2.2) lib/thin/backends/base.rb:57:in `start' thin (1.2.2) lib/thin/server.rb:156:in `start' thin (1.2.2) lib/thin/controllers/controller.rb:80:in `start' thin (1.2.2) lib/thin/runner.rb:174:in `send' thin (1.2.2) lib/thin/runner.rb:174:in `run_command' thin (1.2.2) lib/thin/runner.rb:140:in `run!' thin (1.2.2) bin/thin:6 /opt/local/bin/thin:19:in `load' /opt/local/bin/thin:19
json ruby-on-rails restful-authentication
Optimate Aug 07 '09 at 16:00 2009-08-07 16:00
source share