[英]How to read response body in WAI middleware?
I'm trying to create some middleware that will send 500 errors to a remote server. 我正在尝试创建一些将向远程服务器发送500个错误的中间件。 The error information is in the response body. 错误信息在响应正文中。
How can I get the response body from a Response
as any kind of string? 如何从Response
获取响应主体作为任何字符串? I see responseToStream
but I can't figure out how to use it. 我看到responseToStream
但我不知道如何使用它。
import Network.Wai
import Data.ByteString.Lazy (ByteString)
responseBody :: Response -> IO ByteString
responseBody res = _
An implementation of the comment by @user2407038: @ user2407038的注释的实现:
import Data.IORef (newIORef,modifyIORef',readIORef)
import Data.Monoid ((<>))
import Data.ByteString.Lazy (ByteString)
import Data.ByteString.Builder (toLazyByteString)
import Network.Wai
responseBody :: Response -> IO ByteString
responseBody res =
let (status,headers,body) = responseToStream res in
body $ \f -> do
content <- newIORef mempty
f (\chunk -> modifyIORef' content (<> chunk)) (return ())
toLazyByteString <$> readIORef content
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.