Skip to content

Instantly share code, notes, and snippets.

@unixc3t
Forked from maxivak/readme.md
Created August 21, 2018 03:11
Show Gist options
  • Select an option

  • Save unixc3t/0c46fdd90035a5a570f29942727e6642 to your computer and use it in GitHub Desktop.

Select an option

Save unixc3t/0c46fdd90035a5a570f29942727e6642 to your computer and use it in GitHub Desktop.
Rails. Skip the authenticity token check if its a json request

skip the authenticity token check for a json request

if you got this error while requesting the application via JSON request:

exception ActionController::InvalidAuthenticityToken

backtrace":"[\"/home/uadmin/.rvm/gems/ruby-2.1.7/gems/actionpack-4.2.4/lib/action_controller/metal/request_forgery_protection.rb:181:in `handle_unverified_request'\", 
\"/home/uadmin/.rvm/gems/ruby-2.1.7/gems/actionpack-4.2.4/lib/action_controller/metal/request_forgery_protection.rb:209:in `handle_unverified_request'\", 
...

modify your controller:

class ApplicationController < ActionController::Base
  skip_before_filter :verify_authenticity_token, if: :json_request?

  def json_request?
    request.format.json?
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment