Compojure 路由丢失参数信息

发布于 2024-11-27 05:14:24 字数 705 浏览 0 评论 0原文

我的代码:

(defn json-response [data & [status]]
    {:status (or status 200)
     :headers {"Content-Type" "application/json"}
     :body (json/generate-string data)})

(defroutes checkin-app-handler
  (GET "/:code" [code & more] (json-response {"code" code "params" more})))

当我将文件加载到 repl 并运行此命令时,参数似乎为空:

$ (checkin-app-handler {:server-port 8080 :server-name "127.0.0.1" :remote-addr "127.0.0.1" :uri "/123" :query-string "foo=1&bar=2" :scheme :http :headers {} :request-method :get})
> {:status 200, :headers {"Content-Type" "application/json"}, :body "{\"code\":\"123\",\"params\":{}}"}

我做错了什么?我需要获取查询字符串,但参数映射始终为空。

My code:

(defn json-response [data & [status]]
    {:status (or status 200)
     :headers {"Content-Type" "application/json"}
     :body (json/generate-string data)})

(defroutes checkin-app-handler
  (GET "/:code" [code & more] (json-response {"code" code "params" more})))

When I load the file to the repl and run this command, the params seems to be blank:

$ (checkin-app-handler {:server-port 8080 :server-name "127.0.0.1" :remote-addr "127.0.0.1" :uri "/123" :query-string "foo=1&bar=2" :scheme :http :headers {} :request-method :get})
> {:status 200, :headers {"Content-Type" "application/json"}, :body "{\"code\":\"123\",\"params\":{}}"}

What am I doing wrong? I need to get at the query-string, but the params map is always empty..

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

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

发布评论

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

评论(1

凑诗 2024-12-04 05:14:24

为了将查询字符串解析到 params 映射中,您需要使用 params 中间件:

(ns n
  (:require [ring.middleware.params :as rmp]))

(defroutes checkin-app-routes
  (GET "" [] ...))

(def checkin-app-handler
  (-> #'checkin-app-routes
      rmp/wrap-params
      ; .. other middlewares
      ))

注意,var (#'checkin-app-routes) 的使用并不是绝对必要的,但当您重新定义路由时,它会使包裹在中间件内的路由闭包接受更改。

IOW,您也可以编写

(def checkin-app-handler
  (-> checkin-app-routes
      rmp/wrap-params
      ; .. other middlewares
      ))

,但是在以交互方式重新定义路由时,您也需要重新定义处理程序。

In order to get the query string parsed into the params map, you need to use the params middleware:

(ns n
  (:require [ring.middleware.params :as rmp]))

(defroutes checkin-app-routes
  (GET "" [] ...))

(def checkin-app-handler
  (-> #'checkin-app-routes
      rmp/wrap-params
      ; .. other middlewares
      ))

Note, that the usage of the var (#'checkin-app-routes) is not strictly necessary, but it makes the routes closure, wrapped inside the middlewares, pick up the changes, when you redefine the routes.

IOW you can also write

(def checkin-app-handler
  (-> checkin-app-routes
      rmp/wrap-params
      ; .. other middlewares
      ))

but then, you need to redefine the handler too, when interactively redefining the routes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文