clojureliberator

Liberator: Setting :representation :media-type in :media-type-available?


I wanted to use a decision point :media-type-available? but I failed...

I know I have to set the :representation :media-type but I don't really know how to do it.

So far, my code looks as the following:

(defresource test-resource []
  :media-type-available (fn [req]
    (assoc req :representation {:media-type "application/json"}))
  :available-media-types ["application/json" "text/html"]
  :handle-ok (fn [req] {:ok true})
)

Remarks:

  1. (assoc req :representation {:media-type "application/json"}) produces a correct map with "changed" req object.
  2. in :handle-ok's req object, the :representation value is {}

I have no idea how to set it (it's not a mutable object, is it?) and looking at the liberator's source code doesn't really help...

Thanks, Karol


Solution

  • OK... It was just a typo... Instead of :representation I had :represenation...

    So if anybody interested, there are two ways of solving this:

    1. return req object with merged :representation:

      :media-type-available (fn [req]
        (assoc req :representation {:media-type "application/json"}))
      
    2. return only the :representation object:

      :media-type-available (fn [req]
        {:representation {:media-type "application/json"}})