如何从clojure解放器中的post子句返回401代码?

时间:2022-04-16 22:47:03

I have such a code:

我有这样的代码:

(defresource errors []
         :authorized? (fn [ctx] (authorized? ctx))
         :allowed-methods [:post :options]
         :available-media-types media-types
         :post!      (fn [ctx] (-> ctx
                                   parse-json
                                   errors/insert-error)))

authorized? function checks user token, but I have some other rules, which I want to check inside post! function. How to do it? I can throw an Exception from the post! function, but I would like to return 401 status code.

授权?函数检查用户令牌,但我有一些其他规则,我想检查内部帖子!功能。怎么做?我可以从帖子中抛出异常!功能,但我想返回401状态代码。

1 个解决方案

#1


It's not clear from your question what you're wanting to check in the :post! function, but in the normal case, a failure in the :post! function wouldn't return a 401 error.

从你的问题不清楚你想要检查的内容:帖子!功能,但在正常情况下,失败了:发帖!函数不会返回401错误。

If you want to return a 401 then you should probably be doing your checking of the request in the :authorized? function, and returning true or false from that.

如果你想要返回401,那么你应该在以下方面检查请求:授权?函数,并从中返回true或false。

If you can elaborate on what rules you want to check then my answer might be able to be more specific.

如果您可以详细说明要检查的规则,那么我的答案可能会更加具体。

You probably have already done this, but make sure you understand the Liberator decision graph and how your request flows through it.

您可能已经这样做了,但请确保您了解Liberator决策图以及您的请求如何流经它。

#1


It's not clear from your question what you're wanting to check in the :post! function, but in the normal case, a failure in the :post! function wouldn't return a 401 error.

从你的问题不清楚你想要检查的内容:帖子!功能,但在正常情况下,失败了:发帖!函数不会返回401错误。

If you want to return a 401 then you should probably be doing your checking of the request in the :authorized? function, and returning true or false from that.

如果你想要返回401,那么你应该在以下方面检查请求:授权?函数,并从中返回true或false。

If you can elaborate on what rules you want to check then my answer might be able to be more specific.

如果您可以详细说明要检查的规则,那么我的答案可能会更加具体。

You probably have already done this, but make sure you understand the Liberator decision graph and how your request flows through it.

您可能已经这样做了,但请确保您了解Liberator决策图以及您的请求如何流经它。