# This if full example for your API in Rails 6.1+ that will render errors as JSON instead of ugly HTML error page module API class BaseController < ActionController::API def process_action(*args) super rescue ActionDispatch::Http::Parameters::ParseError => exception # https://github.com/rails/rails/issues/34244#issuecomment-433365579 render status: 400, json: { errors: [exception.message] } rescue ActionDispatch::Http::MimeNegotiation::InvalidType => exception # set valid Content-Type to be able to call render method below request.headers['Content-Type'] = 'application/json' render status: 400, json: { errors: [exception.message] } end # This will work for Rails > 5.2.2 # https://github.com/rails/rails/pull/34341#issuecomment-434727301 rescue_from ActionDispatch::Http::Parameters::ParseError do |exception| render status: 400, json: { errors: [exception.cause.message] } end end end