Compojure Routes失去了params信息

时间:2011-07-30 02:06:40

标签: clojure get query-string compojure

我的代码:

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

当我将文件加载到repl并运行此命令时,参数似乎是空白的:

$ (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\":{}}"}

我做错了什么?我需要得到查询字符串,但是params地图总是空的..

1 个答案:

答案 0 :(得分:5)

为了将查询字符串解析为params映射,您需要使用params中间件:

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

请注意,var(#'checkin-app-routes)的使用并不是绝对必要的,但是当您重新定义路由时,它会使路由闭包,包含在中间件中,然后获取更改。

我也可以写

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

但是,当交互式重新定义路径时,您还需要重新定义处理程序。