Compojure Routes失去了params信息

Man*_*tis 4 get clojure compojure query-string

我的代码:

(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})))
Run Code Online (Sandbox Code Playgroud)

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

$ (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\":{}}"}
Run Code Online (Sandbox Code Playgroud)

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

Ben*_*las 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
      ))
Run Code Online (Sandbox Code Playgroud)

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

你也可以写

(def checkin-app-handler
  (-> checkin-app-routes
      rmp/wrap-params
      ; .. other middlewares
      ))
Run Code Online (Sandbox Code Playgroud)

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