Rails API空请求标头

时间:2018-08-12 20:22:41

标签: ruby-on-rails api http request

从JavaScript向Rails发送请求并向Authorization头提供令牌的情况在我的Rails API上始终显示为空头。

我的所有API控制器都有以下基本代码:

module Api
 class BaseController < ActionController::API
  before_action :require_login

  private

  def require_login
   unless signed_in?
    head :unauthorized
   end
  end

  def signed_in?
    current_user.present?
  end

  def current_user
    if request.headers['Authorization'].present?
      User.find_by(token: request.headers['Authorization'])
    else
      nil
    end
  end
end

结束

在javascript端执行我的提取请求,如下所示:

  fetch(`/api/clients?page=${page}`, {
    headers: {
      Accept: "application/json",
      "Content-Type": "application/json",
      'Authorization': AUTH_TOKEN
    },
    credentials: 'same-origin',
  })

Authorization获取值request.headers总是为零。

有人知道怎么了吗?

0 个答案:

没有答案