如何使用邮递员请求受设计保护的 API? [英] How to use postman to request an API that is protected with devise?

查看:16
本文介绍了如何使用邮递员请求受设计保护的 API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 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

这篇关于如何使用邮递员请求受设计保护的 API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆