我无法从POST请求访问表单参数。我已经尝试了在文档中看到的所有中间件和配置选项的组合,等等(包括不推荐使用的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:
在尝试实现当前两个答案提供的建议时,我遇到了一个新问题:路由匹配过于急切。例如,给定以下内容,由于配置路由中的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)发布于 2017-04-08 14:14:58
问题是,当您以这种方式定义路由时:
(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对于不匹配的URL路径返回nil (例如,它不应该定义not-found路由)。
发布于 2017-04-08 13:53:20
这只是一个猜测,但你有没有尝试过这个:
(defroutes app-routes
api-routes
config-routes
(route/not-found "Not Found"))发布于 2017-04-14 09:37:27
你可能会发现下面的帖子很有用。它讨论了混合应用程序接口和应用程序路由,这样它们就不会相互干扰,并且您可以避免将中间件从一个添加到另一个,等等。Serving app and api routes with different middleware using Ring and Compojure
https://stackoverflow.com/questions/43289975
复制相似问题