【问题标题】:How to get the URL query params in Pedestal?如何获取 Pedestal 中的 URL 查询参数?
【发布时间】:2016-04-15 11:30:27
【问题描述】:

如何将 URL 参数放入 Pedestal 的请求映射中?我假设这需要使用拦截器?然而,基座文件(或严重缺乏)并没有说明这一点。谢谢。

【问题讨论】:

    标签: clojure pedestal


    【解决方案1】:

    通过Pedestal查询参数are parsed automatically,得到的map放在:query-params键下的request map中。

    作为一个简单的例子,从基座服务模板开始,并使用以下定义:

    (defn home-page
      [request]
      (ring-resp/response (format "Hello with params: %s" (:query-params request))))
    
    (defroutes routes
      [[["/" {:get home-page}]]])
    

    现在如果您浏览到http://localhost:8080/?param=true&other=1234,您应该会看到Hello world with paramters: {:param "true", :other "1234"}

    【讨论】:

    • 谢谢。在您提供的链接中,它谈到了“上下文”(例如:当请求与此路由匹配时,拦截器的整个向量都会被推送到上下文中。)我很困惑,当它谈到“上下文”?
    • 不完全是。 context 是在拦截器链中传递的东西,它包含请求映射以及其他项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-22
    • 2016-06-25
    • 2014-08-24
    • 2023-02-21
    • 2018-05-07
    • 2016-06-11
    相关资源
    最近更新 更多