【问题标题】:How to return 401 error as a json format in Clojure Liberator?如何在 Clojure Liberator 中以 json 格式返回 401 错误?
【发布时间】:2016-10-02 16:00:15
【问题描述】:

我将 liberator 用于 REST 服务,我的内容类型是 json。

:authorized?(或其他决定)失败时,它会返回我不想要的text/html

我需要 liberator 为所有类型的错误返回 json 数据格式。

【问题讨论】:

    标签: clojure functional-programming clojurescript compojure liberator


    【解决方案1】:

    Handlers 可用于自定义在不同情况下执行的操作,例如处理正常、未找到资源或未通过身份验证。例如:

    (resource
      :available-media-types ["application/json"]
      :authorized? (fn [ctx] ...)
      :handle-unauthorized {:message "You need to be authenticated"}
      :exists? (fn [ctx] ...)
      :handle-not-found {:message "Resource not found"})
    

    在其他情况下,处理程序可以是一个常量值或将产生它的函数。

    【讨论】:

    • 非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    • 2014-08-24
    • 2018-08-28
    • 2015-08-31
    相关资源
    最近更新 更多