且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ring:以字符串形式读取http请求的正文

更新时间:2023-11-07 17:15:40

您可以使用

You can use ring.util.request/body-string to get the request body as a String.

(body-string request) 

您需要记住, InputStream 只能被读取一次,因此您可能更喜欢将原来的:body 替换为读取的 String 您以后可以再次访问它:

You need to remember that InputStream can be read only once so you might prefer replace the original :body with the read String instead so you can later access it again:

(defn wrap-body-string [handler]
  (fn [request]
    (let [body-str (ring.util.request/body-string request)]
      (handler (assoc request :body (java.io.StringReader. body-str))))))

并添加中间件以包装处理程序:

And add your middleware to wrap your handler:

(def app
  (wrap-body-string handler))