【发布时间】:2017-09-03 12:54:40
【问题描述】:
我无法从 POST 请求访问表单参数。我已经尝试了我在文档、SO 等中看到的中间件和配置选项的每一种组合(包括不推荐使用的 compojure/handler 选项),但我仍然看不到参数。我确定我遗漏了一些非常明显的东西,因此任何建议(无论多么轻微)都将不胜感激。
这是我的最新尝试,我尝试使用 site-defaults 中间件并禁用默认提供的防伪/CSRF 保护。 (我知道这是个坏主意。)但是,当我尝试在 Web 浏览器中查看相关页面时,浏览器会尝试下载该页面,就好像它是一个无法呈现的文件一样。 (有趣的是,使用 Curl 时,页面按预期呈现。)
这是最新的尝试:
(defroutes config-routes*
(POST "/config" request post-config-handler))
(def config-routes
(-> #'config-routes*
(basic-authentication/wrap-basic-authentication authenticated?)
(middleware-defaults/wrap-defaults (assoc middleware-defaults/site-defaults :security {:anti-forgery false}))))
上一次尝试:
(def config-routes
(-> #'config-routes*
(basic-authentication/wrap-basic-authentication authenticated?)
middleware-params/wrap-params))
更新:
参数好像被外层defroutes吞噬了:
(defroutes app-routes
(ANY "*" [] api-routes)
(ANY "*" [] config-routes)
(route/not-found "Not Found"))
所以,我现在的问题变成了:如何将参数通过嵌套的defroutes 传递?
我的临时解决方案是基于this 解决方案,但Steffen Frank's 更简单。我会尝试并跟进。
更新 2:
在尝试实施当前两个答案提供的建议时,我遇到了一个新问题:路由匹配过于急切。例如鉴于以下情况,由于 config-routes 中的 wrap-basic-authentication 中间件,到 /something 的 POST 失败并返回 401 响应。
(defroutes api-routes*
(POST "/something" request post-somethings-handler))
(def api-routes
(-> #'api-routes*
(middleware-defaults/wrap-defaults middleware-defaults/api-defaults)
middleware-json/wrap-json-params
middleware-json/wrap-json-response))
(defroutes config-routes*
(GET "/config" request get-config-handler)
(POST "/config" request post-config-handler))
(def config-routes
(-> #'config-routes*
(basic-authentication/wrap-basic-authentication authenticated?)
middleware-params/wrap-params))
(defroutes app-routes
config-routes
api-routes
(route/not-found "Not Found"))
(def app app-routes)
【问题讨论】: