This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-04-29
Channels
- # announcements (4)
- # architecture (1)
- # beginners (113)
- # boot (17)
- # calva (40)
- # cider (9)
- # clara (1)
- # cljs-dev (21)
- # cljsrn (21)
- # clojure (47)
- # clojure-dev (8)
- # clojure-europe (2)
- # clojure-italy (46)
- # clojure-nl (10)
- # clojure-spec (1)
- # clojure-turkiye (1)
- # clojure-uk (46)
- # clojurescript (102)
- # core-async (21)
- # cursive (35)
- # data-science (1)
- # datomic (7)
- # emacs (2)
- # graphql (1)
- # lumo (15)
- # nrepl (4)
- # nyc (1)
- # off-topic (5)
- # overtone (3)
- # pathom (10)
- # quil (6)
- # re-frame (30)
- # reagent (7)
- # reitit (33)
- # rewrite-clj (1)
- # shadow-cljs (37)
- # spacemacs (73)
- # test-check (3)
- # testing (2)
- # vim (59)
I'm trying to work out how to get JSON out of reitit; I always get back application/octet-stream
, whatever accept
header I use.
Here's my app:
(s/def ::thing (s/keys :req [::id string?]))
(defn get-thing
[{{thing-id :thing-id} :path-params :as _request}]
(response/ok {::id thing-id}))
(defn app
[]
(rring/ring-handler
(rring/router
[""
["/things" {:middleware [[defaults/wrap-defaults defaults/api-defaults]]}
["/:thing-id"
["" {:name ::get-thing
:parameters {:path {:thing-id string?}}
:get {:responses {200 {:body ::thing}}
:handler get-thing}}]]]]
{:data {:coercion rcs/coercion
:muuntaja muuntaja/instance
:middleware [rrmm/format-middleware
rcoercion/coerce-exceptions-middleware
rcoercion/coerce-request-middleware
rcoercion/coerce-response-middleware]}
:exception pretty/exception})
(-> (rring/create-default-handler)
(defaults/wrap-defaults defaults/api-defaults))))
It's my understanding that the reitit.ring.muuntaja.middleware/format-middleware
is the bit that should be responsible for doing content negotiation, and sure enough if I log my request I see this:
:muuntaja/request
{:format "application/json",
:charset "utf-8",
:raw-format "application/json"}
Logging the response after the middleware shows me that the body is an EDN map. What am I missing to convert it to JSON?I wrap the whole app
with muuntaja.middleware/wrap-format
and wrap-defaults
like
(def wrapped-handler
(-> (ring/ring-handler
router
(ring/routes
(create-swagger-ui-handler {:path "/swagger" :jsonEditor true})
(ring/redirect-trailing-slash-handler)
(ring/create-default-handler
{:not-found (constantly {:status 404 :body {:error "Not found"}})})))
wrap-format
(wrap-defaults api-defaults)))
so i think reitit.ring.muuntaja.middleware/format-middleware
does the wrap-format
for me, and i have the api-defaults
in there, unless i've put them in the wrong place
@conan ring-defaults
has wrap-content-type
, which sets the content-type if not present. Sadly, Muuntaja only encodes if there is no content-type, see https://github.com/metosin/muuntaja/issues/96
I think the the ring-defaults
behavior should have a pre-integrated version in reitit, reading the config (at least partially) from route-data.
the wrap-session
has issues, could be solved by doing a reitit.ring.middleware.session/session-middleware
…
so yes, a reitit-driven way of adding the defaults would be a good way of addressing it
The differ uses puget & 4bit ansi, kinda horrible. Could wrap that into the reitit-fipp pretty printer.
I am trying to have routes with subroutes with ring router:
["/withdrawal/{id}" {:get get-withdrawal}
["/confirm" {:post confirm-withdrawal}]]
but this seems to not work for the get endpoint
It doesn't get matched
I tried looking that compiled router
and I see that the route doesn't appear in wildcard routes
@roklenarcic this should work:
["/withdrawal/{id}"
["" {:get get-withdrawal}]
["/confirm" {:post confirm-withdrawal}]]
thank you