简体   繁体   English

Rails:在不呈现页面的情况下发送 401 错误

[英]Rails: send a 401 error without rendering a page

I'm trying to send a 401 authorized from a Rails controller for webhooks, but I can't seem to the right way to do it since I'm not rendering a page.我正在尝试发送从 Rails controller 授权的 401 for webhooks,但我似乎无法以正确的方式执行此操作,因为我没有呈现页面。

render plain: "Unauthorized", status: :unauthorized

throws this error:抛出此错误:

Read error: #<NoMethodError: undefined method `bytesize' for [:error, "Unauthorized"]:Array>

I don't understand why because我不明白为什么因为

render plain: "Unauthorized", status: 400 

works fine.工作良好。

head(:unauthorized)

Returns a response that has no content (merely a status code and headers).返回没有内容的响应(仅是状态代码和标头)。

See ActionController::Head .请参阅ActionController::Head

Are you, by any chance, using this snippet from rails_warden or equivalent middleware?您是否有机会使用rails_warden或等效中间件中的这段代码

If so, the correct stanza is如果是这样,正确的节是

  manager.failure_app = Proc.new { |_env|
    ['401', {'Content-Type' => 'application/json'}, [{ error: 'Unauthorized' }.to_json]]
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM