2011-07-30 2 views
4

マイコード:Compojureルートの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\":{}}"} 

私が間違っているのは何:

(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にファイルをロードし、このコマンドを実行

は、のparamsが空白のようです? paramsマップに解析されたクエリ文字列を取得するためには、あなたがのparamsミドルウェアを使用する必要が

答えて

5

..私は、クエリ文字列で取得する必要がありますが、の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 
    )) 

を書くことができますが、その後、あなたが対話的にルートを再定義するとき、あまりにハンドラを再定義する必要があります。

関連する問題