我正在尝试创建一些将500个错误发送到远程服务器的中间件。错误信息位于响应正文中。
如何从Response
获取响应正文作为任何类型的字符串?我看到responseToStream
但我无法弄清楚如何使用它。
import Network.Wai
import Data.ByteString.Lazy (ByteString)
responseBody :: Response -> IO ByteString
responseBody res = _
答案 0 :(得分:0)
@ 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