Compojure路由丢失params信息 [英] Compojure Routes losing params info
问题描述
我的代码:
(defn json-response [data& [status]]
/ pre>
{:status (或状态200)
:headers {Content-Typeapplication / json}
:body(json / generate-string data)})
-app-handler
(GET/:code[code& more](json-response {codecodeparamsmore})))
当我将文件加载到repl并运行这个命令时,params似乎是空白的:
$(checkin-app-handler {:server-port 8080:server-name127.0.0.1:remote-addr127.0.0.1:uri/ 123:query -stringfoo = 1& bar = 2:scheme:http:headers {}:request-method:get})
> {:status 200,:headers {Content-Typeapplication / json},:body{\code\:\123 \,\params\:{ }}
我做错了什么?我需要得到的查询字符串,但params映射总是空的。
解决方案为了获得查询string parsed into params map,你需要使用params中间件:
(ns n
(:require [ ring.middleware.params:as rmp]))
(defroutes checkin-app-routes
(GET[] ...))
def checkin-app-handler
( - >#'checkin-app-routes
rmp / wrap-params
; ..其他中间件
))
请注意,var(
#'checkin-app-routes
)不是绝对必要的,但它使路由关闭,包裹在中间件内,拾取更改,当你重新定义路由。
IOW你可以也写
(def checkin-app-handler
( - > checkin-app-routes
rmp / wrap-params
; ..其他中间件
))
那么,当交互式地重新定义路由时,您需要重新定义处理程序。
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..
解决方案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.
这篇关于Compojure路由丢失params信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!