clojure liberator - returning json from a put request -
i struggling return json put! request:
my code looks this:
(defn body-as-string [ctx] (if-let [body (get-in ctx [:request :body])] (condp instance? body java.lang.string body (slurp (io/reader body))))) (defn orbit-world [dimensions ctx] (let [in (json/parse-string (body-as-string ctx))] (json/generate-string in))) (defn init-world [params] (let [dimensions (integer/parseint params) world (vec (repeat dimensions (vec (take dimensions (repeatedly #(rand-int 2))))))] (json/generate-string world))) (defresource world [dimensions] :allowed-methods [:get :put] :available-media-types ["application/json"] :available-charsets ["utf-8"] :handle-ok (fn [_] (init-world dimensions)) :put! (fn [ctx] (orbit-world dimensions ctx)))
i want return passed put request json until understand going on.
but if make put request, following response:
http/1.1 201 created
date: sun, 18 may 2014 15:35:32 gmt
content-type: text/plain
content-length: 0
server: jetty(7.6.8.v20121106)
my request returns json don't understand why put request not/
that because successfull put request not return http 200 status code (at least according liberator), returns http 201 status code, can see response. liberator handle http status code each in different handler. in order achieve want, have do:
(defresource world [dimensions] :allowed-methods [:get :put] :available-media-types ["application/json"] :available-charsets ["utf-8"] :handle-ok (fn [_] (init-world dimensions)) :put! (fn [ctx] (orbit-world dimensions ctx)) :handle-created (fn [_] (init-world dimensions))) ; handler other.
since declare none on :handle-created, defaults empty string text/plain content-type.
edit:
in order understand more, have see decision graph. in there, can see after handling put!
goes decision handling new?
, if it's true go handle-created
if false, go respond-with-entity?
, on.
Comments
Post a Comment