weavejester / compojure

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

Missing info about site-defaults in "Destructuring Syntax" wiki page #158

Closed jumarko closed 7 years ago

jumarko commented 7 years ago

The example given on Destructuring Syntax page is missing the information that it's necessary to wrap handler with site-defaults or api-defaults to make destructuring works for query params:

(GET "/user/:id" [id greeting]
  (str "<h1>" greeting " user " id "</h1>"))

I've found following stackoverflow thread: http://stackoverflow.com/questions/8963213/accessing-compojure-query-string They say it was handled in defroutes macro automatically but it's no longer the case.

jiacai2050 commented 7 years ago

which version did you test ?

I test this using version 1.5.1, and destructuring works as expected.

jumarko commented 7 years ago

I used:

                 [ring/ring-core "1.5.1"]
                 [ring/ring-defaults "0.2.3"]

Is there any chance that you have wrap-defaults already in place? I didn't use any special lein template just lein new ring-app and then add the dependencies and necessary configuration. (I did this as an exercise for "Web Development with Clojure, 2nd ed." book)

Following is my minimal core.clj:

(ns ring-app.core
  (:require [ring.adapter.jetty :as jetty]
            [ring.middleware.reload :refer [wrap-reload]]
            [ring.middleware.defaults :refer [wrap-defaults site-defaults]]
            [compojure.core :as c]))

(c/defroutes handler
  ;; example from https://github.com/weavejester/compojure/wiki/Destructuring-Syntax
  (c/GET "/foo/:id" [id greeting] (str "<h1>" greeting " user " id " </h1>"))
  )

(defn -main []
  (jetty/run-jetty
   (-> handler
       #_(wrap-defaults site-defaults))
   {:port 3000
    :join? false}))

Go to http://localhost:3000/foo/juraj?greeting=Ahoj and you will see only "user juraj". Uncomment (wrap-defaults site-defaults) and you will see "Ahoj user juraj".

weavejester commented 7 years ago

Yes, Compojure doesn't add Ring middleware itself, you need to either do that manually, or use something like Ring-Defaults.

There is a "prerequisite knowledge" section at the top of the destructuring page warning that an understanding of Ring is necessary, but maybe I can make this even more explicit.

weavejester commented 7 years ago

I've updated the Wiki with a warning about using the middleware the examples use.

jumarko commented 7 years ago

Great, thanks.