ring.middleware and compojure: params with keywords

感情迁移 提交于 2019-12-10 17:45:06

问题


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


回答1:


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))))


来源:https://stackoverflow.com/questions/26151801/ring-middleware-and-compojure-params-with-keywords

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!