我无法从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中间件,POST / to something失败并返回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)
答案 0 :(得分:2)
问题在于,当您以这种方式定义路线时:
(defroutes app-routes
(ANY "*" [] api-routes)
(ANY "*" [] config-routes)
(route/not-found "Not Found"))
然后只要返回非零响应,任何请求都将由api-routes
匹配。因此api-routes
不会吞下你的请求参数,而是窃取整个请求。
相反,您应该将app-routes
定义为(首选解决方案):
(defroutes app-routes
api-routes
config-routes
(route/not-found "Not Found"))
或确保您的api-routes
针对不匹配的网址路径返回nil(例如,它不应该定义not-found
路由)。
答案 1 :(得分:1)
只是一个猜测,但你试过这个:
(defroutes app-routes
api-routes
config-routes
(route/not-found "Not Found"))
答案 2 :(得分:0)
您可能会发现以下帖子很有用。它讨论了混合API和应用程序路由,以便它们不会相互干扰,并且您避免将中间件从一个添加到另一个等等。Serving app and api routes with different middleware using Ring and Compojure