This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-08-03
Channels
- # beginners (98)
- # boot (18)
- # chestnut (2)
- # cider (90)
- # cljdoc (3)
- # cljs-dev (1)
- # clojure (64)
- # clojure-dev (14)
- # clojure-dusseldorf (4)
- # clojure-italy (11)
- # clojure-nl (5)
- # clojure-spec (9)
- # clojure-uk (69)
- # clojurescript (63)
- # code-reviews (2)
- # core-logic (20)
- # cursive (13)
- # datomic (52)
- # dirac (2)
- # emacs (4)
- # figwheel (6)
- # hyperfiddle (13)
- # luminus (4)
- # nrepl (1)
- # off-topic (7)
- # onyx (9)
- # overtone (3)
- # parinfer (3)
- # pedestal (1)
- # re-frame (31)
- # reagent (74)
- # reitit (34)
- # rum (3)
- # shadow-cljs (51)
- # spacemacs (22)
- # specter (7)
- # tools-deps (23)
- # uncomplicate (3)
- # vim (9)
i'm still confused a little about the role of muuntaja. it only does content type negotiation, right? so if for example an endpoint can serve both html and json (which are obviously very different) i still have to handle that in my handler right?
@vale Muuntaja is about body parameter encoding and decoding - and negotiating about formats it supports. By default it supports json, edn and transit format. So, if you are sending & expecting those, it works behind the scenes without much notice - just read :body
params as clojure data and return response :body
as clojure data as.
For some reason, it doesn’t publish the negotiataion results for formats it doesn’t cover. e.g. if the client expects a text/html
response, the handler doesn’t get a hint of it - need to parse the headers itself.
((muuntaja.middleware/wrap-format-negotiate identity)
{:headers {"content-type" "application/transit+json"
"accept" "application/edn"}})
;{:headers {"content-type" "application/transit+json", "accept" "application/edn"},
; :muuntaja/request #FormatAndCharset{:format "application/transit+json", :charset "utf-8"},
; :muuntaja/response #FormatAndCharset{:format "application/edn", :charset "utf-8"}}
that’s the basic usage. wrap-format
contains all of: wrap-format-negotiate
, wrap-request-format
and wrap-response-format
.
((muuntaja.middleware/wrap-format-negotiate identity)
{:headers {"accept" "text/html"}})
;{:headers {"accept" "text/html"},
; :muuntaja/request nil,
; :muuntaja/response #FormatAndCharset{:format "application/json", :charset "utf-8"}}
but… if you want to return alway text/html
from an endpoint, just set the [:headers "Content-Type"]
yourself, format the body accordingly and muuntaja doesn’t touch it.
the reitit-middleware
is merged in master & in clojars. The new guide: https://metosin.github.io/reitit/ring/default_middleware.html
and this dies with a Malformed application/json in :muuntaja/encode
if it's wrapped with wrap-format
few things:
• the :produces
is not handled by anything
• if you are returning text/html
, you need to set the header yourself
{:get {:swagger {:produces #{"text/html"}} ;; not mandatory, just for docs
:handler (constantly
{:status 200
:headers {"Content-Type" "text/html"}
:body "<h1>hello</h1>"})}}
without the swagger-hint:
{:get (constantly
{:status 200
:headers {"Content-Type" "text/html"}
:body "<h1>hello</h1>"})}
yes, it's just a a documentation hint now. There is a good sample app here: https://github.com/metosin/reitit/blob/master/examples/ring-swagger/src/example/server.clj
For instance, I want to have a default resource handler, but I also want to have a default handler that returns the index.html file of my SPA.