gpt4 book ai didi

clojure - Compojure 路由丢失参数信息

转载 作者:行者123 更新时间:2023-11-30 23:46:14 26 4
gpt4 key购买 nike

我的代码:

(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\":{}}"}

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

最佳答案

为了将查询字符串解析为 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
))

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

关于clojure - Compojure 路由丢失参数信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6880641/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com