访问 Compojure 查询字符串
我试图从 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要将处理程序包装在
compojure.handler/api
或compojure.handler/site
中,以添加适当的中间件来访问:query-params< /代码>。这曾经在
defroutes
中自动发生,但现在不再这样了。执行此操作后,{params :query-params}
解构形式将导致params
绑定到{"foo" "bar"}
当您使用foo=bar
作为查询字符串点击/remservice
时。(或者您可以手动添加
wrap-params
等 - 这些位于各种ring.middleware.*
命名空间中;请参阅compojure.handler
的代码 (链接到 Compojure 中的相关文件1.0.1) 的名称。)例如,
如果您现在点击
http://localhost:8080/remservice?foo=bar
,您应该看到{"foo" "bar"}< /code> ——解析为 Clojure 映射的查询字符串的文本表示。
You'll need to wrap your handler in
compojure.handler/api
orcompojure.handler/site
to add appropriate middleware to gain access to:query-params
. This used to happen automagically indefroutes
, but no longer does. Once you do that, the{params :query-params}
destructuring form will causeparams
to be bound to{"foo" "bar"}
when you hit/remservice
withfoo=bar
as the query string.(Or you could add in
wrap-params
etc. by hand -- these reside in variousring.middleware.*
namespaces; see the code ofcompojure.handler
(link to the relevant file in Compojure 1.0.1) for their names.)E.g.
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.在 compojure 1.2.0 的默认应用程序中,querystring 中间件似乎默认包含在内。您可以检查该请求。
它应该有很多东西,包括 params 键。
因此,您可以包含标准 Clojure 解构表单来访问响应中的查询参数。
您的页面应如下所示。
然而,正如上面 Michal 的评论中所指出的,键被转换为字符串,如果您想访问它们,您需要使用 get 函数而不是更方便的符号查找。
In the default app for compojure 1.2.0, the querystring middleware seems included by default. You can inspect the request as such.
It should have a lot of stuff, including the
params
key.As such, you can include a standard Clojure destructuring form to access the query parameters in your response.
Your page should then look like the following.
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.
使用
compojure 1.6.1
HTTP-request-destructuring 以这样的方式为我工作:中添加
(因为[ring/ring-defaults "0.3.2"]
:project.clj
中的 dependencycompojure.handler
命名空间自 1.2 起已被弃用,转而使用[ring-defaults]
)your.routes 的
:require
中添加[ring.middleware.defaults :refer :all]
。命名空间your.routes.namespace
中添加(def site (wrap-defaults app site-defaults))
,其中app
是通过声明(defroutes app ...
project.clj
中添加:ring {:handler your.routes.namespace/site}
With
compojure 1.6.1
HTTP-request-destructuring works for me in a such way:[ring/ring-defaults "0.3.2"]
in:dependencies
inproject.clj
(becausecompojure.handler
namespace was deprecated since 1.2 in favor of the[ring-defaults]
)[ring.middleware.defaults :refer :all]
in:require
inyour.routes.namespace
(def site (wrap-defaults app site-defaults))
inyour.routes.namespace
, whereapp
is declared via(defroutes app ...
:ring {:handler your.routes.namespace/site}
inproject.clj
我很幸运在 compojure 1.1.5 中不需要包装器并且能够使用
:as
指令I had luck in compojure 1.1.5 not needing a wrapper and being able to use the
:as
directive