How to extract a path-param from a Reitit backend end route

1.6k views Asked by At

How do you get a path-param out of a Reitit backend Clojure route? I am trying to get the val associated with :id in the following manner, but keep getting a 404 file not found error in the REPL.

["/foo/:id" {:get 
  (fn [{:keys [path-params ]}]
   (some-ns/some-fn (:id path-params)))}]

I have tried using the documentation at https://luminusweb.com/docs/routes.html and https://github.com/metosin/reitit/blob/master/README.md.

1

There are 1 answers

0
CambodianCoder On

Destructure the path params from the request, and then extract the id.

(defn project-routes
  "SPA routing"
  []
  ["" {:middleware [middleware/wrap-base
                    middleware/wrap-formats]}

   ["/foo"
    ["/bar/:id"
     {:get (fn [{:keys [path-params] :as _req}]
             (http-response/ok 
                   (core/READ some-table 
                      (Integer/valueOf (:id path-params)))))}]]])

Documentation and examples obtained from https://github.com/luminus-framework/luminus-docs/blob/master/resources/md/sessions_cookies.md. Controllers were used, as documented by https://clojureverse.org/t/how-do-you-accomplish-spa-re-frame-teardown/5516/6.