Compojure:如何映射查询参数

发布于 2024-12-10 17:51:30 字数 865 浏览 0 评论 0 原文

我正在尝试使以下任何映射能够映射 http://mysite.org/add?http://sitetoadd.comhttp://mysite.org/ add?u=http://sitetoadd.com

  (GET "/add?:url" [url] url)
  (GET "/add?u=:url" [url] url)
  (GET "/add" {params :params} (params :u))
  (GET "/add" {params :params} (params "u"))
  (GET "/add" [u] u)

但它失败了,我不知道为什么。另一方面,这是可行的:

  (GET "/add/:url" [url] url)

但我无法使用它,因为我必须传递一个网址,并且 http://mysite.org/add/http://sitetoadd.com 无效,而http://mysite.org/add?http://sitetoadd.com 可以。

编辑:转储请求我发现参数为空。我认为它会包含 POST GET 参数,但我可以找到我传递的参数的唯一地方是 :query-string ("u=asd") 。 似乎需要一个中间件来解析查询字符串。顺便说一句,我的问题仍然存在。

I'm trying to make any of the following mappings work to map http://mysite.org/add?http://sitetoadd.com or http://mysite.org/add?u=http://sitetoadd.com

  (GET "/add?:url" [url] url)
  (GET "/add?u=:url" [url] url)
  (GET "/add" {params :params} (params :u))
  (GET "/add" {params :params} (params "u"))
  (GET "/add" [u] u)

But it just fails and I don't know why. On the other hand, this works:

  (GET "/add/:url" [url] url)

but I can't use it because I have to pass a url and http://mysite.org/add/http://sitetoadd.com is invalid while http://mysite.org/add?http://sitetoadd.com is ok.

EDIT: dumping request i've seen that params is empty. I thought that it would contain POST and GET parameters, but the only place where I can find the params I pass is in :query-string ("u=asd"). It seems that a middleware is needed to parse query strings. My question stands still, by the way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花细雨 2024-12-17 17:51:30

请参阅“重大更改”下的 https://github.com/weavejester/compojure。默认情况下,参数映射不再受约束。如果您的示例路由位于“(defroutes main-routes ... )”内,请确保通过“(handler/site main-routes)”激活它,如 https://github.com/weavejester/compojure/wiki/Getting-Started 因为它是确保 params 映射得到绑定的站点或 api 方法经过 默认。

这是一个有效的示例:

(ns hello-world
  (:use compojure.core, ring.adapter.jetty)
  (:require [compojure.route :as route]
            [compojure.handler :as handler]))

(defroutes main-routes
  (GET "/" {params :params} (str "<h1>Hello World</h1>" (pr-str params)))
  (route/not-found "<h1>Page not found</h1>"))

(defn -main [& m]
  (run-jetty (handler/site main-routes) {:port 8080}))

See https://github.com/weavejester/compojure under Breaking Changes. The params map is no longer bound by default. If you have your example routes inside a "(defroutes main-routes ... )", make sure you activate it through "(handler/site main-routes)" as explained on https://github.com/weavejester/compojure/wiki/Getting-Started as it is the site or api method that makes sure the params map gets bound by default.

Here's an example that works:

(ns hello-world
  (:use compojure.core, ring.adapter.jetty)
  (:require [compojure.route :as route]
            [compojure.handler :as handler]))

(defroutes main-routes
  (GET "/" {params :params} (str "<h1>Hello World</h1>" (pr-str params)))
  (route/not-found "<h1>Page not found</h1>"))

(defn -main [& m]
  (run-jetty (handler/site main-routes) {:port 8080}))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文