gpt4 book ai didi

ruby-on-rails - 使用 Rails 4 响应未授权 (401) 状态

转载 作者:数据小太阳 更新时间:2023-10-29 06:43:21 25 4
gpt4 key购买 nike

给定以下 Rails 4.2 Controller :

class Api::UsersController < ApplicationController
def index
respond_to do |format|
format.html do
flash[:error] = 'Access denied'
redirect_to root_url
end
format.json do
render json: {}, status: :unauthorised
end
end
end
end

当使用 RSpec 3 时,我尝试调用此 index 操作并期望状态为 401,而我的状态始终为 200。

我得到 401 的唯一时刻是用 head 401 替换 index 操作内容,但我想用错误 401 进行响应并构建一个“不错”的正文,如 { error: 401, message: 'Unauthorised' }

为什么 status::unauthorised 被忽略了?

最佳答案

使用错误代码而不是它的名称:渲染 json:{},状态:401

关于ruby-on-rails - 使用 Rails 4 响应未授权 (401) 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28119606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com