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.










share|improve this question























  • 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














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.










share|improve this question























  • 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












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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














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














































































Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo