【问题标题】:Compojure Routes losing params infoCompojure Routes 丢失参数信息
【发布时间】:2011-10-16 09:01:17
【问题描述】:

我的代码:

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

我做错了什么?我需要获取查询字符串,但参数映射始终为空..

【问题讨论】:

    标签: clojure get query-string compojure


    【解决方案1】:

    为了将查询字符串解析成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
          ; .. other middlewares
          ))
    

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

    IOW你也可以写

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

    但是,在交互式地重新定义路由时,您也需要重新定义处理程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-20
      • 2016-05-05
      • 1970-01-01
      相关资源
      最近更新 更多