Clojure如何处理POST请求正文? (http-kit,compojure)

时间:2020-03-23 01:36:45

标签: clojure compojure http-kit

我有一个带有登录表单的页面,以及一个接受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”}] [: {}] [:headers {“ origin”“ http://localhost:3449”,“主机”“ localhost:8000”,“用户代理”“ Mozilla / 5.0(Windows NT 10.0; WOW64)AppleWebKit / 537.36(KHTML,like Gecko)Chrome / 74.0.3729.172 Safari / 537.36 Vivaldi / 2.5.1525.46”,“内容类型”,“ application / x-www-form-urlencoded”,“ content-length”,“ 27”,“ referer”,“ {{3 }}”,“连接”,“保持活动”,“不安全升级请求”,“ 1”,“接受”,“ text / html,application / xhtml + xml,application / xml; q = 0.9,image / webp, image / apng, / ; q = 0.8,application / signed-exchange; v = b3“,” accept-language“” zh-cn,en; q = 0.9“,” accept-encoding“ “ gzip,deflate,br”,“缓存控制”“ 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“]] [:服务器端口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]“]] [:scheme:http] [:request-method:post]

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

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

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

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

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

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

0 个答案:

没有答案
相关问题