Responding json Luminus Clojure
up vote
1
down vote
favorite
I'm taking luminus for a spin. It is confusing. How do I manage to give a json formatted response? My understanding from the docs is that if I set the content-type as json, then the wrap-format middleware should see that and know how to serialize the data. However when I set the content-type I get "java.lang.IllegalStateException: Can't coerce body of type class clojure.lang.PersistentArrayMap"
After trying lots of things this is what I understand should work:
(defroutes home-routes
(GET "/"
(home-page))
(GET "/docs"
(-> (response/ok (-> "docs/docs.md" io/resource slurp))
(response/header "Content-Type" "text/plain; charset=utf-8")))
(POST "/upload" [file]
(->(response/ok (assoc :inserted (->> (upload-file resource-path file)
load-sheet
persist-sheet
count-ones
)
)
)
(response/header "Content-Type" "application/json")
)
)
)
This is my current setup of wrap-formats:
(defn wrap-formats [handler]
(let [wrapped (-> handler wrap-params (wrap-format formats/instance))]
(fn [request]
;; disable wrap-formats for websockets
;; since they're not compatible with this middleware
((if (:websocket? request) handler wrapped) request))))
and this is formats/instance:
(def instance
(m/create
(-> m/default-options
(assoc-in
[:formats "application/json" :opts :modules]
[(JodaModule.)])
(assoc-in
[:formats "application/transit+json" :encode-opts]
:handlers org.joda.time.DateTime joda-time-writer))))
What am I failing to see?
Thanks in advance.
clojure luminus
add a comment |
up vote
1
down vote
favorite
I'm taking luminus for a spin. It is confusing. How do I manage to give a json formatted response? My understanding from the docs is that if I set the content-type as json, then the wrap-format middleware should see that and know how to serialize the data. However when I set the content-type I get "java.lang.IllegalStateException: Can't coerce body of type class clojure.lang.PersistentArrayMap"
After trying lots of things this is what I understand should work:
(defroutes home-routes
(GET "/"
(home-page))
(GET "/docs"
(-> (response/ok (-> "docs/docs.md" io/resource slurp))
(response/header "Content-Type" "text/plain; charset=utf-8")))
(POST "/upload" [file]
(->(response/ok (assoc :inserted (->> (upload-file resource-path file)
load-sheet
persist-sheet
count-ones
)
)
)
(response/header "Content-Type" "application/json")
)
)
)
This is my current setup of wrap-formats:
(defn wrap-formats [handler]
(let [wrapped (-> handler wrap-params (wrap-format formats/instance))]
(fn [request]
;; disable wrap-formats for websockets
;; since they're not compatible with this middleware
((if (:websocket? request) handler wrapped) request))))
and this is formats/instance:
(def instance
(m/create
(-> m/default-options
(assoc-in
[:formats "application/json" :opts :modules]
[(JodaModule.)])
(assoc-in
[:formats "application/transit+json" :encode-opts]
:handlers org.joda.time.DateTime joda-time-writer))))
What am I failing to see?
Thanks in advance.
clojure luminus
I think you should first check if there is a middleware (in a lib) that does what you want. i.e. Convert a response map to json and sets the appropriate headers.
– nakiya
yesterday
For example, see github.com/ring-clojure/ring-json#wrap-json-response
– nakiya
yesterday
@nakiya isnt that the wrap-formats part i pasted. meaning shouldnt that be taken care by that middleware
– jstuartmilne
18 hours ago
1
yogthos (Dmitri Sotnikov) is active on Clujorians Slack and has a channel for Luminus.
– manandearth
12 hours ago
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm taking luminus for a spin. It is confusing. How do I manage to give a json formatted response? My understanding from the docs is that if I set the content-type as json, then the wrap-format middleware should see that and know how to serialize the data. However when I set the content-type I get "java.lang.IllegalStateException: Can't coerce body of type class clojure.lang.PersistentArrayMap"
After trying lots of things this is what I understand should work:
(defroutes home-routes
(GET "/"
(home-page))
(GET "/docs"
(-> (response/ok (-> "docs/docs.md" io/resource slurp))
(response/header "Content-Type" "text/plain; charset=utf-8")))
(POST "/upload" [file]
(->(response/ok (assoc :inserted (->> (upload-file resource-path file)
load-sheet
persist-sheet
count-ones
)
)
)
(response/header "Content-Type" "application/json")
)
)
)
This is my current setup of wrap-formats:
(defn wrap-formats [handler]
(let [wrapped (-> handler wrap-params (wrap-format formats/instance))]
(fn [request]
;; disable wrap-formats for websockets
;; since they're not compatible with this middleware
((if (:websocket? request) handler wrapped) request))))
and this is formats/instance:
(def instance
(m/create
(-> m/default-options
(assoc-in
[:formats "application/json" :opts :modules]
[(JodaModule.)])
(assoc-in
[:formats "application/transit+json" :encode-opts]
:handlers org.joda.time.DateTime joda-time-writer))))
What am I failing to see?
Thanks in advance.
clojure luminus
I'm taking luminus for a spin. It is confusing. How do I manage to give a json formatted response? My understanding from the docs is that if I set the content-type as json, then the wrap-format middleware should see that and know how to serialize the data. However when I set the content-type I get "java.lang.IllegalStateException: Can't coerce body of type class clojure.lang.PersistentArrayMap"
After trying lots of things this is what I understand should work:
(defroutes home-routes
(GET "/"
(home-page))
(GET "/docs"
(-> (response/ok (-> "docs/docs.md" io/resource slurp))
(response/header "Content-Type" "text/plain; charset=utf-8")))
(POST "/upload" [file]
(->(response/ok (assoc :inserted (->> (upload-file resource-path file)
load-sheet
persist-sheet
count-ones
)
)
)
(response/header "Content-Type" "application/json")
)
)
)
This is my current setup of wrap-formats:
(defn wrap-formats [handler]
(let [wrapped (-> handler wrap-params (wrap-format formats/instance))]
(fn [request]
;; disable wrap-formats for websockets
;; since they're not compatible with this middleware
((if (:websocket? request) handler wrapped) request))))
and this is formats/instance:
(def instance
(m/create
(-> m/default-options
(assoc-in
[:formats "application/json" :opts :modules]
[(JodaModule.)])
(assoc-in
[:formats "application/transit+json" :encode-opts]
:handlers org.joda.time.DateTime joda-time-writer))))
What am I failing to see?
Thanks in advance.
clojure luminus
clojure luminus
edited Nov 9 at 14:24
jas
6,66321632
6,66321632
asked Nov 9 at 12:42
jstuartmilne
2,24511018
2,24511018
I think you should first check if there is a middleware (in a lib) that does what you want. i.e. Convert a response map to json and sets the appropriate headers.
– nakiya
yesterday
For example, see github.com/ring-clojure/ring-json#wrap-json-response
– nakiya
yesterday
@nakiya isnt that the wrap-formats part i pasted. meaning shouldnt that be taken care by that middleware
– jstuartmilne
18 hours ago
1
yogthos (Dmitri Sotnikov) is active on Clujorians Slack and has a channel for Luminus.
– manandearth
12 hours ago
add a comment |
I think you should first check if there is a middleware (in a lib) that does what you want. i.e. Convert a response map to json and sets the appropriate headers.
– nakiya
yesterday
For example, see github.com/ring-clojure/ring-json#wrap-json-response
– nakiya
yesterday
@nakiya isnt that the wrap-formats part i pasted. meaning shouldnt that be taken care by that middleware
– jstuartmilne
18 hours ago
1
yogthos (Dmitri Sotnikov) is active on Clujorians Slack and has a channel for Luminus.
– manandearth
12 hours ago
I think you should first check if there is a middleware (in a lib) that does what you want. i.e. Convert a response map to json and sets the appropriate headers.
– nakiya
yesterday
I think you should first check if there is a middleware (in a lib) that does what you want. i.e. Convert a response map to json and sets the appropriate headers.
– nakiya
yesterday
For example, see github.com/ring-clojure/ring-json#wrap-json-response
– nakiya
yesterday
For example, see github.com/ring-clojure/ring-json#wrap-json-response
– nakiya
yesterday
@nakiya isnt that the wrap-formats part i pasted. meaning shouldnt that be taken care by that middleware
– jstuartmilne
18 hours ago
@nakiya isnt that the wrap-formats part i pasted. meaning shouldnt that be taken care by that middleware
– jstuartmilne
18 hours ago
1
1
yogthos (Dmitri Sotnikov) is active on Clujorians Slack and has a channel for Luminus.
– manandearth
12 hours ago
yogthos (Dmitri Sotnikov) is active on Clujorians Slack and has a channel for Luminus.
– manandearth
12 hours ago
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53225926%2fresponding-json-luminus-clojure%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
I think you should first check if there is a middleware (in a lib) that does what you want. i.e. Convert a response map to json and sets the appropriate headers.
– nakiya
yesterday
For example, see github.com/ring-clojure/ring-json#wrap-json-response
– nakiya
yesterday
@nakiya isnt that the wrap-formats part i pasted. meaning shouldnt that be taken care by that middleware
– jstuartmilne
18 hours ago
1
yogthos (Dmitri Sotnikov) is active on Clujorians Slack and has a channel for Luminus.
– manandearth
12 hours ago