如何在WAI中间件中读取响应体?

时间:2017-08-02 17:42:44

标签: haskell middleware haskell-wai

我正在尝试创建一些将500个错误发送到远程服务器的中间件。错误信息位于响应正文中。

如何从Response获取响应正文作为任何类型的字符串?我看到responseToStream但我无法弄清楚如何使用它。

import Network.Wai
import Data.ByteString.Lazy (ByteString)

responseBody :: Response -> IO ByteString
responseBody res = _

1 个答案:

答案 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