首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Compojure中使用嵌套解冻时无法访问表单参数

在Compojure中使用嵌套解冻时无法访问表单参数
EN

Stack Overflow用户
提问于 2017-04-08 11:49:36
回答 3查看 94关注 0票数 1

我无法从POST请求访问表单参数。我已经尝试了在文档中看到的所有中间件和配置选项的组合,等等(包括不推荐使用的compojure/handler选项),但我仍然看不到参数。我肯定我遗漏了一些非常明显的东西,所以任何建议(无论多么轻微)都会非常感谢。

这是我的最新尝试,我尝试使用site-defaults中间件并禁用默认提供的防伪造/CSRF保护。(我知道这不是一个好主意。)但是,当我尝试在web浏览器中查看有问题的页面时,浏览器会尝试下载该页面,就好像它是无法呈现的文件一样。(有趣的是,在使用Curl时,页面会按预期呈现。)

这是最新的尝试:

代码语言:javascript
复制
(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}))))

之前的尝试:

代码语言:javascript
复制
(def config-routes
  (-> #'config-routes*
    (basic-authentication/wrap-basic-authentication authenticated?)
    middleware-params/wrap-params))

更新:参数似乎被外部defroutes吞噬了

代码语言:javascript
复制
(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响应。

代码语言:javascript
复制
(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)
EN

回答 3

Stack Overflow用户

发布于 2017-04-08 14:14:58

问题是,当您以这种方式定义路由时:

代码语言:javascript
复制
(defroutes app-routes
  (ANY "*" [] api-routes)
  (ANY "*" [] config-routes)
  (route/not-found "Not Found"))

那么任何请求都将被api-routes匹配,只要它返回非空响应。因此,api-routes不会吞噬您的请求参数,而是窃取整个请求。

相反,您应该将app-routes定义为(首选解决方案):

代码语言:javascript
复制
(defroutes app-routes
  api-routes
  config-routes
  (route/not-found "Not Found"))

或者确保您的api-routes对于不匹配的URL路径返回nil (例如,它不应该定义not-found路由)。

票数 2
EN

Stack Overflow用户

发布于 2017-04-08 13:53:20

这只是一个猜测,但你有没有尝试过这个:

代码语言:javascript
复制
(defroutes app-routes
   api-routes
   config-routes
   (route/not-found "Not Found"))
票数 1
EN

Stack Overflow用户

发布于 2017-04-14 09:37:27

你可能会发现下面的帖子很有用。它讨论了混合应用程序接口和应用程序路由,这样它们就不会相互干扰,并且您可以避免将中间件从一个添加到另一个,等等。Serving app and api routes with different middleware using Ring and Compojure

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43289975

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档