Ruby on rails "exception": "#<ActionController::InvalidAuthenticityToken: ActionController::InvalidAuthenticityToken>" error solution for post data using postman or http request - UCS - Unleash-Coding-Skills

Tuesday, 14 August 2018

Ruby on rails "exception": "#" error solution for post data using postman or http request - UCS



While performing a test on getting a response for a particular URL by sending post data using a postman or some other ways. You may encounter an error. The data will not be saved and it will return an error like this.

{
    "status": 422,
    "error": "Unprocessable Entity",
    "exception": "#<ActionController::InvalidAuthenticityToken: ActionController::InvalidAuthenticityToken>",
    "traces": {
        "Application Trace": [],
        "Framework Trace": [
            {
                "id": 0,
                "trace": "actionpack (5.2.1) lib/action_controller/metal/request_forgery_protection.rb:211:in `handle_unverified_request'"
            },
            {
                "id": 1,
                "trace": "actionpack (5.2.1) lib/action_controller/metal/request_forgery_protection.rb:243:in `handle_unverified_request'"
            },
            {
                "id": 2,
                "trace": "actionpack (5.2.1) lib/action_controller/metal/request_forgery_protection.rb:238:in `verify_authenticity_token'"
            },
            {
                "id": 3,
                "trace": "activesupport (5.2.1) lib/active_support/callbacks.rb:426:in `block in make_lambda'"
            },
            {
                "id": 4,
                "trace": "activesupport (5.2.1) lib/active_support/callbacks.rb:198:in `block (2 levels) in halting'"
            },
            {
                "id": 5,
                "trace": "actionpack (5.2.1) lib/abstract_controller/callbacks.rb:34:in `block (2 levels) in <module:Callbacks>'"
            },
            {
                "id": 6,
                "trace": "activesupport (5.2.1) lib/active_support/callbacks.rb:199:in `block in halting'"
            },
            {
                "id": 7,
                "trace": "activesupport (5.2.1) lib/active_support/callbacks.rb:513:in `block in invoke_before'"
            },   
  .............
        ]
    }
}

. . . .

The error is due to rails can't verify CSRF authenticity when making a POST request.

To get rid of the error we need to add this

line in ApplicationController.rb file like this. This is for rails version 4 or 5.

class ApplicationController < ActionController::Base                                                                             
   skip_before_action :verify_authenticity_token                                                                                      

end                                                                                                                                                             

or you can also add the other line based on the rails version  less than or equal to 3

class ApplicationController < ActionController::Base                                                                             
   skip_before_filter :verify_authenticity_token                                                                                        
end                                                                                                                                                             

If in case you using rails 5 you can make use of rails as an API directly so they do not include the CSRF middleware and many other components that cause these errors by using this command to create a new application.

rails new appname --api

Tags:

 Ruby on rails "exception": "#<ActionController::InvalidAuthenticityToken: ActionController::InvalidAuthenticityToken>" error  solution for post data using postman or http request, CSRF authencity error in ruby on rails 4,  CSRF authencity error in ruby on rails 5,error while sending the post data in rails,security error while sending the error in post data.

No comments:

Post a comment