lostisland/faraday · error · ArgumentError

Unexpected params received (got #{params.size} instead of 1)

Error message

Unexpected params received (got #{params.size} instead of 1)

What it means

Faraday::Request::Authorization builds the Authorization header value in header_from. The contract is: for the 'basic' scheme exactly two params (user, password) are required and are base64-joined; for every other scheme exactly one param is required — a value, a Proc, or any object responding to #call, optionally taking env to compute the token lazily. Any other count raises ArgumentError telling you how many params were received.

Source

Thrown at lib/faraday/request/authorization.rb:39

      # @param env [Faraday::Env]
      def on_request(env)
        return if env.request_headers[KEY]

        env.request_headers[KEY] = header_from(@type, env, *@params)
      end

      private

      # @param type [String, Symbol]
      # @param env [Faraday::Env]
      # @param params [Array]
      # @return [String] a header value
      def header_from(type, env, *params)
        if type.to_s.casecmp('basic').zero? && params.size == 2
          Utils.basic_header_from(*params)
        elsif params.size != 1
          raise ArgumentError, "Unexpected params received (got #{params.size} instead of 1)"
        else
          value = params.first
          if (value.is_a?(Proc) && value.arity == 1) || (value.respond_to?(:call) && value.method(:call).arity == 1)
            value = value.call(env)
          elsif value.is_a?(Proc) || value.respond_to?(:call)
            value = value.call
          end
          "#{type} #{value}"
        end
      end
    end
  end
end

Faraday::Request.register_middleware(authorization: Faraday::Request::Authorization)

View on GitHub (pinned to b25b1b26cc)

Solutions

  1. For non-basic schemes pass exactly one value: conn.request :authorization, 'Bearer', token.
  2. Pre-join multiple segments yourself: "#{token_type} #{token}" or join values into one string before passing.
  3. For basic auth pass exactly two params: conn.request :authorization, 'Basic', 'username', 'password' — or use conn.set_basic_auth(user, pass).
  4. When the value must be computed per request, pass a single proc: conn.request :authorization, 'Bearer', ->(env) { token_for(env) }.

Example fix

# before
conn.request :authorization, 'Bearer', access_token, refresh_token
# => ArgumentError: Unexpected params received (got 2 instead of 1)

# after
conn.request :authorization, 'Bearer', access_token
# computed per request:
conn.request :authorization, 'Bearer', ->(env) { auth_store.token_for(env) }
# basic stays two-arg:
conn.request :authorization, 'Basic', 'user', 'pass'
Defensive patterns

Strategy: validation

Validate before calling

expected = type.to_s.casecmp('basic').zero? ? 2 : 1
raise ArgumentError, "auth expects #{expected} value(s)" unless auth_values.size == expected
conn.request :authorization, type, *auth_values

Type guard

def valid_auth_params?(type, values)
  type.to_s.casecmp('basic').zero? ? values.size == 2 : values.size == 1
end

Try / catch

begin
  conn.request :authorization, 'Bearer', *auth_args
rescue ArgumentError => e
  raise unless e.message.include?('Unexpected params')
  conn.request :authorization, 'Bearer', auth_args.join(' ')
end

Prevention

When it happens

Trigger: conn.request :authorization, 'Bearer', 'token1', 'token2' (two values for a non-basic scheme); conn.request :authorization, 'Bearer' with no value (zero params); conn.request :authorization, 'Basic', 'user' (basic with one param instead of user+password); splatting an array of auth parts: conn.request :authorization, 'Bearer', *parts where parts.size != 1.

Common situations: Migrating from the deprecated conn.authorization / conn.basic_auth helper API and assuming extra args are concatenated; copying curl -H examples with multiple segments; dynamic token code where the token list can be empty; passing user and password to a Bearer scheme by mistake.

Related errors


AI-assisted analysis of lostisland/faraday@b25b1b26cc (2026-08-21). Data as JSON: /api/errors/dba174ccdb653e6f. Report an issue: GitHub.