weavejester / compojure

A concise routing library for Ring/Clojure
Eclipse Public License 1.0
4.08k stars 259 forks source link

Route parameter destructuring not working #218

Closed winpat closed 1 year ago

winpat commented 1 year ago

First things first, thank you for your awesome libraries!

I am trying to get the first code sample of the following wiki page working:

Given the following code sample:

(defn handler [id]
  (println id))

(defroutes app
  (GET "/:id" [id] handler))

(defn -main []
  (server/run-server (wrap-defaults app site-defaults) {:port 8000 :ip "0.0.0.0" }))

When making the following request:

curl localhost:8000/12

I would expect the handler to receive "12" as the only argument. Instead I get the entire request map.

Deps used are:

compojure/compojure {:mvn/version "1.7.0"}}
ring/ring-defaults {:mvn/version "0.3.4"}

Is this intended? Am I doing something wrong?

weavejester commented 1 year ago

You're not calling handler with id. So what you want is:

(defroutes app
  (GET "/:id" [id] (handler id)))

Instead, you're returning the hander function from the route. When this happens, Compojure treats it as a Ring handler. In other words:

(GET "/example" _ handler)

Is identical to:

(GET "/example" request (handler request))
winpat commented 1 year ago

Thank you for your explanation! This makes total sense now.