Search code examples
clojurecompojurering

ring.middleware and compojure: params with keywords


In my web handler, I have the following defined:

(:require ... 

[ring.middleware.cookies :refer [wrap-cookies]]
            [ring.middleware.multipart-params :refer [wrap-multipart-params]]
            [ring.middleware.params :refer [wrap-params]]
            [ring.middleware.keyword-params :refer [wrap-keyword-params]]
            [ring.middleware.content-type :refer [wrap-content-type]]
            [ring.middleware.format-response :refer [wrap-restful-response]
...)

(def app
  (-> (routes home/my-routes)
      (wrap-cookies)
      (wrap-params)
      (wrap-multipart-params)
      (wrap-keyword-params))))

Everything works. Testing with curl using a URL that looks like "../test?foo=123" gives me a params map that looks like {:foo 123}. However, what appears as a keyword is actually a string: (keyword? :foo) returns false.

I've tried rearranging the handlers and removing them one at a time, but to no avail. Is there something about compojure that is converting the keywords back into strings? Thanks


Solution

  • wrap-keyword-params middleware should be run after wrap-params and wrap-multipart-params, so your app should look like:

    (def app
      (-> (routes home/my-routes)
          (wrap-keyword-params)
          (wrap-cookies)
          (wrap-params)
          (wrap-multipart-params))))