Fork me on GitHub
#reitit
<
2022-01-11
>
mikerod19:01:53

How do allow routes to have query params when defined via reitit.ring/ring-handler ? eg.

(require '[reitit.ring :as rr])

(def handler
  (rr/ring-handler (rr/router ["/a" {:get (fn [{:keys [uri]}] {:status 200 :body uri})}]
                              {})
                   {}
                   {}))


(handler {:request-method :get
          :uri "/a"})
;;= {:status 200, :body "/a"}

(handler {:request-method :get
          :uri "/a?x=10"})
;;= nil
I know the docs mention you can give them via:
(handler {:request-method :get
          :uri "/a"
          :query-params {"x" 10}})
However, that isn’t useful when the handler is your top-level app dispatched from the server. I’ve tried middleware such as reitit.ring.middleware.parameters/parameters-middleware, but it still isn’t what I need. It looks like the :uri itself needs to have the ?x=10 part removed prior to getting to the router for route matching - since the router is sensitive to anything other than the path.

mikerod19:01:15

I don’t see any examples of making a server where your routes can optionally have query params (could even be ignored). Update: I think the answer may be that it is assumed a higher-level middleware/adapter catches it - such as the ring.util.servletring.adapter.jetty/run-jetty wrapping.

ccann20:01:19

I’m new to reitit and I’ve added a couple routes to https://github.com/metosin/reitit/blob/master/examples/frontend-re-frame/src/cljs/frontend_re_frame/core.cljs but when I go to e.g. http://localhost:8280/authorize by typing it in the URL bar it always takes me to the home page at http://localhost:8280/. But if I dispatch the event the routing works… any idea what I’m missing?

ccann20:01:42

ah, it’s because I wasn’t using the # in the path facepalm