如何编写一个拦截器,从get/post请求中提取json使用基座



我正在做一个简单的API,这将需要从json/edn请求读取体参数我试图让程序回显内容作为edn对象,但有些东西似乎不工作这里是我的路由

(def routes
(route/expand-routes
#{["/echo" :get [body-params/body-params print-response]  :route-name :greet]}))

拦截器


(def print-response {:name ::print-response
:leave (fn [context]
(let [content-type (get-in context [:request :content-type])
updated-response (assoc (-> context :response) :headers {"Content-Type" content-type}
:status  200
:body (get-in context [:request] :edn-params))]
(assoc context :response updated-response))
)
} 


)

修复:在body-params周围加上括号解释实际上body-params是一个高阶函数,它创建了一个拦截器所以它必须被命名为


(def routes
(route/expand-routes
#{["/echo" :get [(body-params/body-params) print-response]  :route-name :greet]}))


最新更新