ring:以字符串形式读取http请求的正文 [英] ring: read body of a http request as string
问题描述
在环形服务器内部处理http请求时,请求数据的正文存储在键:body
中的request-hashmap中.例如,如下所示:
When handling a http request inside a ring server, the body of the request-data is stored in the request-hashmap in the key :body
. For instance as followed:
#object[org.eclipse.jetty.server.HttpInputOverHTTP 0x2d88a9aa "HttpInputOverHTTP@2d88a9aa"]
以防万一,我只是对原始文本感兴趣,我是如何读取该对象的?
In case, I'm just interested in the raw text, how an I read out this object?
推荐答案
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))
这篇关于ring:以字符串形式读取http请求的正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!