0
votes

My routes.rb -

Rails.application.routes.draw do
  root 'welcome#index'

  namespace :api, defaults: { format: :json } do
    namespace :v1 do
      get "/users/:cv_id" => "engine#users_get"
      post "/users" => "engine#users_add"
    end
  end

and my main controller is --

class Api::V1::MainController < ApplicationController

  http_basic_authenticate_with name: ENV["API_USERNAME"], password: ENV["API_PASSWORD"]

  API_CMDS_LIST = {
    "Engine"  => [:users_add, :users_update],
  }

  include Api::V1::ErrorMessage
  skip_before_filter :verify_authenticity_token
  protect_from_forgery with: :null_session

  def resulting_json(resp)
    if resp[:result] == "error" && !resp[:code]
      resp[:code] = Api::V1::ErrorMessage::CODES[resp[:message]] || Api::V1::ErrorMessage::DEFAULT_CODE
    end

    render json: resp
  end

  def authenticate
    #authenticate_or_request_with_http_basic do |user, password|

      #File.write('test.txt', "USER: #{user}. PASS: #{password}")
      #@user && password == @user.secret_token #&&  @user.belongs_to_active_brand?
      true
    #end
  end

  def validate_request
    action = params[:action]
    klass = API_CMDS_LIST.select{ |klass, methods| methods.include?(action.to_sym) }.keys[0]

    @req = "Api::V1::#{klass}".constantize.new(send("# {action}_params").merge( {user: @user} ), request)

    if [email protected]?
      err_msg = @req.errors.full_messages.first
      code = Api::V1::ErrorMessage::CODES[err_msg] || Api::V1::ErrorMessage::DEFAULT_CODE
      render json: { result: "error", msg: err_msg, code: code } and   return
    end
  end

end

and engine controller code is -

class Api::V1::EngineController < Api::V1::MainController
  include Api::V1::ErrorMessage
  before_filter :validate_request

  API_CMDS_LIST["Engine"].each do |method|

    define_method(method) do
      response = @req.send(method)
      resulting_json(response)
    end
  end

  def users_add_params
    params.permit(:first_name, :last_name) 
  end
end

but when i try to hit the endpoint "/users" with params {first_name : "satyam",last_name:"agrawal"} it gives me this error ---

Processing by Api::V1::EngineController#users_add as JSON Parameters: {"first_name"=>"satyam", "last_name"=>"agrawal"} Completed 500 Internal Server Error in 1ms (ActiveRecord: 0.0ms)

TypeError (no implicit conversion of nil into String):

activesupport (5.0.1) lib/active_support/security_utils.rb:23:in digest' activesupport (5.0.1) lib/active_support/security_utils.rb:23:inhexdigest' activesupport (5.0.1) lib/active_support/security_utils.rb:23:in variable_size_secure_compare' actionpack (5.0.1) lib/action_controller/metal/http_authentication.rb:75:inblock (2 levels) in http_basic_authenticate_with' actionpack (5.0.1) lib/action_controller/metal/http_authentication.rb:97:in call' actionpack (5.0.1) lib/action_controller/metal/http_authentication.rb:97:inauthenticate' actionpack (5.0.1) lib/action_controller/metal/http_authentication.rb:87:in authenticate_with_http_basic' actionpack (5.0.1) lib/action_controller/metal/http_authentication.rb:83:inauthenticate_or_request_with_http_basic' actionpack (5.0.1) lib/action_controller/metal/http_authentication.rb:71:in block in http_basic_authenticate_with' activesupport (5.0.1) lib/active_support/callbacks.rb:396:ininstance_exec' activesupport (5.0.1) lib/active_support/callbacks.rb:396:in block in make_lambda' activesupport (5.0.1) lib/active_support/callbacks.rb:169:incall' activesupport (5.0.1) lib/active_support/callbacks.rb:169:in block (2 levels) in halting' actionpack (5.0.1) lib/abstract_controller/callbacks.rb:12:incall' actionpack (5.0.1) lib/abstract_controller/callbacks.rb:12:in block (2 levels) in <module:Callbacks>' activesupport (5.0.1) lib/active_support/callbacks.rb:170:incall' activesupport (5.0.1) lib/active_support/callbacks.rb:170:in block in halting' activesupport (5.0.1) lib/active_support/callbacks.rb:454:incall' activesupport (5.0.1) lib/active_support/callbacks.rb:454:in block in call' activesupport (5.0.1) lib/active_support/callbacks.rb:454:ineach' activesupport (5.0.1) lib/active_support/callbacks.rb:454:in call' activesupport (5.0.1) lib/active_support/callbacks.rb:101:inrun_callbacks' activesupport (5.0.1) lib/active_support/callbacks.rb:750:in _run_process_action_callbacks' activesupport (5.0.1) lib/active_support/callbacks.rb:90:inrun_callbacks' actionpack (5.0.1) lib/abstract_controller/callbacks.rb:19:in process_action' actionpack (5.0.1) lib/action_controller/metal/rescue.rb:20:inprocess_action' actionpack (5.0.1) lib/action_controller/metal/instrumentation.rb:32:in block in process_action' activesupport (5.0.1) lib/active_support/notifications.rb:164:inblock in instrument' activesupport (5.0.1) lib/active_support/notifications/instrumenter.rb:21:in instrument' activesupport (5.0.1) lib/active_support/notifications.rb:164:ininstrument' actionpack (5.0.1) lib/action_controller/metal/instrumentation.rb:30:in process_action' actionpack (5.0.1) lib/action_controller/metal/params_wrapper.rb:248:inprocess_action' activerecord (5.0.1) lib/active_record/railties/controller_runtime.rb:18:in process_action' actionpack (5.0.1) lib/abstract_controller/base.rb:126:inprocess' actionview (5.0.1) lib/action_view/rendering.rb:30:in process' actionpack (5.0.1) lib/action_controller/metal.rb:190:indispatch' actionpack (5.0.1) lib/action_controller/metal.rb:262:in dispatch' actionpack (5.0.1) lib/action_dispatch/routing/route_set.rb:50:indispatch' actionpack (5.0.1) lib/action_dispatch/routing/route_set.rb:32:in serve' actionpack (5.0.1) lib/action_dispatch/journey/router.rb:39:inblock in serve' actionpack (5.0.1) lib/action_dispatch/journey/router.rb:26:in each' actionpack (5.0.1) lib/action_dispatch/journey/router.rb:26:inserve' actionpack (5.0.1) lib/action_dispatch/routing/route_set.rb:725:in call' rack (2.0.1) lib/rack/etag.rb:25:incall' rack (2.0.1) lib/rack/conditional_get.rb:38:in call' rack (2.0.1) lib/rack/head.rb:12:incall' rack (2.0.1) lib/rack/session/abstract/id.rb:222:in context' rack (2.0.1) lib/rack/session/abstract/id.rb:216:incall' actionpack (5.0.1) lib/action_dispatch/middleware/cookies.rb:613:in call' activerecord (5.0.1) lib/active_record/migration.rb:553:incall' actionpack (5.0.1) lib/action_dispatch/middleware/callbacks.rb:38:in block in call' activesupport (5.0.1) lib/active_support/callbacks.rb:97:inrun_callbacks' activesupport (5.0.1) lib/active_support/callbacks.rb:750:in _run_call_callbacks' activesupport (5.0.1) lib/active_support/callbacks.rb:90:inrun_callbacks' actionpack (5.0.1) lib/action_dispatch/middleware/callbacks.rb:36:in call' actionpack (5.0.1) lib/action_dispatch/middleware/executor.rb:12:incall' actionpack (5.0.1) lib/action_dispatch/middleware/remote_ip.rb:79:in call' actionpack (5.0.1) lib/action_dispatch/middleware/debug_exceptions.rb:49:incall' web-console (3.4.0) lib/web_console/middleware.rb:135:in call_app' web-console (3.4.0) lib/web_console/middleware.rb:28:inblock in call' web-console (3.4.0) lib/web_console/middleware.rb:18:in catch' web-console (3.4.0) lib/web_console/middleware.rb:18:incall' actionpack (5.0.1) lib/action_dispatch/middleware/show_exceptions.rb:31:in call' railties (5.0.1) lib/rails/rack/logger.rb:36:incall_app' railties (5.0.1) lib/rails/rack/logger.rb:24:in block in call' activesupport (5.0.1) lib/active_support/tagged_logging.rb:69:inblock in tagged' activesupport (5.0.1) lib/active_support/tagged_logging.rb:26:in tagged' activesupport (5.0.1) lib/active_support/tagged_logging.rb:69:intagged' railties (5.0.1) lib/rails/rack/logger.rb:24:in call' sprockets-rails (3.2.0) lib/sprockets/rails/quiet_assets.rb:13:incall' actionpack (5.0.1) lib/action_dispatch/middleware/request_id.rb:24:in call' rack (2.0.1) lib/rack/method_override.rb:22:incall' rack (2.0.1) lib/rack/runtime.rb:22:in call' activesupport (5.0.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:incall' actionpack (5.0.1) lib/action_dispatch/middleware/executor.rb:12:in call' actionpack (5.0.1) lib/action_dispatch/middleware/static.rb:136:incall' rack (2.0.1) lib/rack/sendfile.rb:111:in call' railties (5.0.1) lib/rails/engine.rb:522:incall' puma (3.7.1) lib/puma/configuration.rb:232:in call' puma (3.7.1) lib/puma/server.rb:578:inhandle_request' puma (3.7.1) lib/puma/server.rb:415:in process_client' puma (3.7.1) lib/puma/server.rb:275:inblock in run' puma (3.7.1) lib/puma/thread_pool.rb:120:in call' puma (3.7.1) lib/puma/thread_pool.rb:120:inblock in spawn_thread'

please help me to sort it out

1
You ask for help and pasted all that code that you want someone to take the time to read through and you can't even take the time to format it properly? Think again about how you ask for help.margo

1 Answers

2
votes

With the logs you have given it looks like it's failing on main_controller.rb

http_basic_authenticate_with name: ENV["API_USERNAME"], password: ENV["API_PASSWORD"]

One way to debug this is going into your rails console and check if your environment variables are set.

rails console

ENV['API_USERNAME']
  => 'api_username'

ENV['API_PASSWORD']
  => 'hash1234090909'