如何使用邮递员请求受设计保护的 API?邮递员、如何使用、API

2023-09-07 10:35:05 作者:早安晚安祝作业入土为安

我有一个使用 devise 的 rails 应用程序,起初它只是使用 rails.现在我正在使用 gem react-rails 添加一些反应.. 简而言之,我有一个这样的 API 控制器

I have a rails app using devise, in the beggining it was just using rails. Now i am adding some react using the gem react-rails.. To make the story short i have an API controller like this

class Api::CustomersController < ApplicationController
skip_before_action :verify_authenticity_token
before_action :authenticate_user!

def index
    customers = Customer.all
    render json: customers, status: :ok
end ....

我想访问该信息或使用 POSTMAN 测试我的 API.当然,当我尝试应用程序时,只需将我重定向到登录页面,因为before_action :authenticate_user!".现在的问题是:我可以使用邮递员访问该 API URL?如何?

and i want to access that info or test my API using POSTMAN.. Of course when i try the app just redirect me to the login page because of the "before_action :authenticate_user!".. Now the question is: Can i access to that API URL using postman? how?

提前致谢

推荐答案

这主要与devise无关.

您需要为每个 user 生成一个 auth_token(当然,为它创建一个具有唯一性的数据库迁移)并使用 对您的用户进行身份验证授权标头.

You'll need to generate an auth_token for each user (and of course, create a db migration for it with uniqueness) and authenticate your user using Authorization header.

否则,您可以从此处检查 devise 令牌解决方案:https://github.com/plataformatec/devise/wiki/How-To:-Simple-Token-Authentication-Example

Otherwise, you can check the devise tokens solutions from here: https://github.com/plataformatec/devise/wiki/How-To:-Simple-Token-Authentication-Example