Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Clojure 在Compojure中使用嵌套的defroutes时无法访问表单参数_Clojure_Compojure_Ring - Fatal编程技术网

Clojure 在Compojure中使用嵌套的defroutes时无法访问表单参数

Clojure 在Compojure中使用嵌套的defroutes时无法访问表单参数,clojure,compojure,ring,Clojure,Compojure,Ring,我无法从POST请求访问表单参数。我尝试了在文档中看到的中间件和配置选项的所有组合,等等(包括不推荐的compojure/handler选项),但仍然无法看到参数。我确信我遗漏了一些非常明显的东西,所以任何建议(无论多么轻微)都将不胜感激 下面是我的最新尝试,我尝试使用SiteDefaults中间件并禁用默认提供的防伪/CSRF保护。(我知道这是个坏主意。)然而,当我试图在web浏览器中查看相关页面时,浏览器会尝试下载该页面,就好像它是一个无法呈现的文件一样。(有趣的是,当使用Curl时,页面会

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

下面是我的最新尝试,我尝试使用SiteDefaults中间件并禁用默认提供的防伪/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

我的临时解决方案基于解决方案,但要简单得多。我会试试看,然后跟进

更新2:

在尝试实现当前两个答案提供的建议时,我遇到了一个新问题:路由匹配过于频繁。e、 g.鉴于以下情况,由于配置路由中的wrap基本身份验证中间件,发送到/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)

只是一个猜测,但是你试过这个吗:

(defroutes app-routes
   api-routes
   config-routes
   (route/not-found "Not Found"))

问题在于,当您以这种方式定义路线时:

(defroutes app-routes
  (ANY "*" [] api-routes)
  (ANY "*" [] config-routes)
  (route/not-found "Not Found"))
然后,只要返回非nil响应,任何请求都将被
api路由
匹配。因此,
api路由
不会吞下您的请求参数,而是窃取整个请求

相反,您应该将
应用程序路由定义为(首选解决方案):


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

您可能会发现以下帖子很有用。它讨论了混合api和应用程序路由,这样它们就不会相互干扰,并且可以避免将中间件从一个路由添加到另一个路由等等。

谢谢,这是一个非常干净的解决方案。我最终使用了这种方法,但我会尝试一下。这很有效。。。某种程度上。如果
api-routes
排在列表的第一位,那么它匹配/接受了请求,我回到了开始的位置。我的两个嵌套defroutes都没有
notfound
处理程序,所以我不明白为什么会发生这种情况。感谢您将其组合在一起。请看我对Steffen答案的回答,但“首选解决方案”似乎是顺序相关的,这是我想要避免的。当
api路由
排在第一位但没有找到匹配项时,我是否需要以某种方式显式地“为不匹配的URL路径返回nil?”很难说您是否不共享
api路由
的实现。另外,
defroutes
将创建一个处理函数,如果其路由与请求不匹配,则返回
nil
。您的
api路由
似乎与您认为不应该的请求相匹配。我已使用基于最新迭代的示例更新了我的问题。不过,我担心我已经开始违反“什么是好问题”的指导方针。
(defroutes app-routes
  api-routes
  config-routes
  (route/not-found "Not Found"))