2

我有一个带有登录表单的页面,以及一个接受 POST 请求的服务器。这是服务器:

(ns clj_server.core
  (:require [org.httpkit.server :refer [run-server]]
            [compojure.core :refer [defroutes POST]]
            [compojure.route :as route]
            [ring.middleware.params :refer [wrap-params]]))

(defn printPostBody [request]
  {:status 200
   :headers {"Content-Type" "text/html"}
   :body request})

(defroutes routes
  (POST "/login" request (printPostBody request))
  (route/not-found {:status 404 :body "<h1>Page not found</h1"}))

(def app (wrap-params routes))

(defn -main [& args]
  (run-server app {:port 8000})
  (println "Server started on port 8000"))

当我发出登录请求时,会打印出来:

[:remote-addr "0:0:0:0:0:0:0:1"][:params {"username" "asdf", "password" "ghkj"}][:route-params {}] [:headers {"origin" " http://localhost:3449 ", "host" "localhost:8000", "user-agent" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, 像 Gecko ) Chrome/74.0.3729.172 Safari/537.36 Vivaldi/2.5.1525.46", "content-type" "application/x-www-form-urlencoded", "content-length" "27", "referer" " http:// localhost:3449/ ", "连接" "keep-alive", "升级不安全请求" "1", "accept" "text/html,application/xhtml+xml,application/xml;q=0.9,image/ webp,图像/apng,/;q=0.8,application/signed-exchange;v=b3", "accept-language" "en-US,en;q=0.9", "accept-encoding" "gzip, deflate, br", "cache-control " "max-age=0"}][:async-channel #object[org.httpkit.server.AsyncChannel 0x2212125a "/0:0:0:0:0:0:0:1:8000<->/0 :0:0:0:0:0:0:1:50592"]][:server-port 8000][:content-length 27][:form-params {"username" "asdf", "password" " ghkj"}][:compojure/route [:post "/login"]][:websocket?false][:query-params {}][:content-type "application/x-www-form-urlencoded"][ :character-encoding "utf8"][:uri "/login"][:server-name "localhost"][:query-string nil][:body #object[org.httpkit.BytesInputStream 0x4e67c6c0 "BytesInputStream[len=27 ]"]][:方案:http][:请求方法:post]

所以我想知道,它是什么样的数据结构?它看起来不像散列图,但是当我打印出来(:params request)而不是 时request,我得到

[“用户名”“asdf”][“密码”“ghkj”]

它是向量列表的哈希图吗?我不明白我在这里处理什么样的数据结构。

另外,当我只要求参数而不是整个请求时,为什么会{"username" "asdf", "password" "ghkj"}被转换为?["username" "asdf"]["password" "ghkj"]

然后我尝试打印出来(get (:params request) "username"),我得到了“asdf”。这是有道理的,但它如何允许我get在多个向量的集合上使用?

最后,我将如何处理我的 post 请求中的 JSON?它只是同一件事,还是我必须以不同的方式处理它?

4

0 回答 0