Compojure Routes losing params info

这一生的挚爱 提交于 2019-12-23 15:29:05

问题


My code:

(defn json-response [data & [status]]
    {:status (or status 200)
     :headers {"Content-Type" "application/json"}
     :body (json/generate-string data)})

(defroutes checkin-app-handler
  (GET "/:code" [code & more] (json-response {"code" code "params" more})))

When I load the file to the repl and run this command, the params seems to be blank:

$ (checkin-app-handler {:server-port 8080 :server-name "127.0.0.1" :remote-addr "127.0.0.1" :uri "/123" :query-string "foo=1&bar=2" :scheme :http :headers {} :request-method :get})
> {:status 200, :headers {"Content-Type" "application/json"}, :body "{\"code\":\"123\",\"params\":{}}"}

What am I doing wrong? I need to get at the query-string, but the params map is always empty..


回答1:


In order to get the query string parsed into the params map, you need to use the params middleware:

(ns n
  (:require [ring.middleware.params :as rmp]))

(defroutes checkin-app-routes
  (GET "" [] ...))

(def checkin-app-handler
  (-> #'checkin-app-routes
      rmp/wrap-params
      ; .. other middlewares
      ))

Note, that the usage of the var (#'checkin-app-routes) is not strictly necessary, but it makes the routes closure, wrapped inside the middlewares, pick up the changes, when you redefine the routes.

IOW you can also write

(def checkin-app-handler
  (-> checkin-app-routes
      rmp/wrap-params
      ; .. other middlewares
      ))

but then, you need to redefine the handler too, when interactively redefining the routes.



来源:https://stackoverflow.com/questions/6880641/compojure-routes-losing-params-info

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