clojurefunctional-programmingclojurescriptcompojureliberator

How to return 401 error as a json format in Clojure Liberator?


I'm using liberator for REST service and my content type is json.

When :authorized?(or other decisions) fails it returns text/html which i don't want.

I need liberator to return json data format for all type of errors.


Solution

  • Handlers can be used to customise what to do in different cases like handle ok, resource is not found or user is not authenticated. For example:

    (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"})
    

    As in other cases, handler can be a constant value or a function that will produce it.