访问 Compojure 查询字符串

发布于 2024-12-28 03:25:47 字数 373 浏览 5 评论 0原文

我试图从 url 查询字符串中提取一个值,但是我可以返回我认为是地图的内容,但是当我使用下面的代码时,它不会按预期处理它。谁能建议我如何访问返回的查询字符串数据结构中的特定值?

http://localhost:8080/remservice?foo=bar

(defroutes my-routes
  (GET "/" [] (layout (home-view)))
  (GET "/remservice*" {params :query-params} (str (:parameter params))))

I'm trying to pull a value out of the url query string however I can return what I believe is a map, however when i use the below code, it doesn't process it as expected. Can anyone advise how I access specific values in the returned querystring datastructure?

http://localhost:8080/remservice?foo=bar

(defroutes my-routes
  (GET "/" [] (layout (home-view)))
  (GET "/remservice*" {params :query-params} (str (:parameter params))))

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

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

发布评论

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

评论(4

离笑几人歌 2025-01-04 03:25:47

您需要将处理程序包装在 compojure.handler/apicompojure.handler/site 中,以添加适当的中间件来访问 :query-params< /代码>。这曾经在 defroutes 中自动发生,但现在不再这样了。执行此操作后,{params :query-params} 解构形式将导致 params 绑定到 {"foo" "bar"} 当您使用 foo=bar 作为查询字符串点击 /remservice 时。

(或者您可以手动添加 wrap-params 等 - 这些位于各种 ring.middleware.* 命名空间中;请参阅compojure.handler的代码 (链接到 Compojure 中的相关文件1.0.1) 的名称。)

例如,

(defroutes my-routes
  (GET "/remservice*" {params :query-params}
       (str params)))

(def handler (-> my-routes compojure.handler/api))

; now pass #'handler to run-jetty (if that's what you're using)

如果您现在点击 http://localhost:8080/remservice?foo=bar,您应该看到 {"foo" "bar"}< /code> ——解析为 Clojure 映射的查询字符串的文本表示。

You'll need to wrap your handler in compojure.handler/api or compojure.handler/site to add appropriate middleware to gain access to :query-params. This used to happen automagically in defroutes, but no longer does. Once you do that, the {params :query-params} destructuring form will cause params to be bound to {"foo" "bar"} when you hit /remservice with foo=bar as the query string.

(Or you could add in wrap-params etc. by hand -- these reside in various ring.middleware.* namespaces; see the code of compojure.handler (link to the relevant file in Compojure 1.0.1) for their names.)

E.g.

(defroutes my-routes
  (GET "/remservice*" {params :query-params}
       (str params)))

(def handler (-> my-routes compojure.handler/api))

; now pass #'handler to run-jetty (if that's what you're using)

If you now hit http://localhost:8080/remservice?foo=bar, you should see {"foo" "bar"} -- the textual representation of your query string parsed into a Clojure map.

晨曦慕雪 2025-01-04 03:25:47

在 compojure 1.2.0 的默认应用程序中,querystring 中间件似乎默认包含在内。您可以检查该请求。

(GET "/" request (str request))

它应该有很多东西,包括 params 键。

{ . . .  :params {:key1 "value1" :key2 "value2} . . . }

因此,您可以包含标准 Clojure 解构表单来访问响应中的查询参数。

(GET "/" {params :params} (str params))

您的页面应如下所示。

{"key1" "value1", "key2" "value2"}

然而,正如上面 Michal 的评论中所指出的,键被转换为字符串,如果您想访问它们,您需要使用 get 函数而不是更方便的符号查找。

(GET "/" {params :params} (get params "key1"))

;;the response body should be "value1"

In the default app for compojure 1.2.0, the querystring middleware seems included by default. You can inspect the request as such.

(GET "/" request (str request))

It should have a lot of stuff, including the params key.

{ . . .  :params {:key1 "value1" :key2 "value2} . . . }

As such, you can include a standard Clojure destructuring form to access the query parameters in your response.

(GET "/" {params :params} (str params))

Your page should then look like the following.

{"key1" "value1", "key2" "value2"}

As noted in the comment by Michal above, however, the keys are converted to strings and if you'd like to access them you need to use the get function rather than the more convenient symbol lookups.

(GET "/" {params :params} (get params "key1"))

;;the response body should be "value1"
思念满溢 2025-01-04 03:25:47

使用 compojure 1.6.1 HTTP-request-destructuring 以这样的方式为我工作:

  1. 中添加 [ring/ring-defaults "0.3.2"]project.clj 中的 dependency (因为 compojure.handler 命名空间自 1.2 起已被弃用,转而使用[ring-defaults])
  2. your.routes 的 :require 中添加 [ring.middleware.defaults :refer :all] 。命名空间
  3. your.routes.namespace 中添加 (def site (wrap-defaults app site-defaults)),其中 app 是通过 声明(defroutes app ...
  4. project.clj 中添加 :ring {:handler your.routes.namespace/site}

With compojure 1.6.1 HTTP-request-destructuring works for me in a such way:

  1. add [ring/ring-defaults "0.3.2"] in :dependencies in project.clj (because compojure.handler namespace was deprecated since 1.2 in favor of the [ring-defaults])
  2. add [ring.middleware.defaults :refer :all] in :require in your.routes.namespace
  3. add (def site (wrap-defaults app site-defaults)) in your.routes.namespace, where app is declared via (defroutes app ...
  4. add :ring {:handler your.routes.namespace/site} in project.clj
方觉久 2025-01-04 03:25:47

我很幸运在 compojure 1.1.5 中不需要包装器并且能够使用 :as 指令

(GET "/tweet/:folder/:detail" [folder detail :as req]
  (twitter-controller/tweet folder detail (-> req :params :oauth_verifier))

I had luck in compojure 1.1.5 not needing a wrapper and being able to use the :as directive

(GET "/tweet/:folder/:detail" [folder detail :as req]
  (twitter-controller/tweet folder detail (-> req :params :oauth_verifier))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文