From 2ccf13d185e26d4cb4a51622e748ec64336435f4 Mon Sep 17 00:00:00 2001 From: Bartosz Nitka Date: Mon, 15 Jun 2020 02:08:26 +0200 Subject: [PATCH] Fix encoding error in toResponse The old code uses `Data.ByteString.Char8.pack` which destroys unicode. Example: ``` ghci> Data.ByteString.Char8.pack "\9679" "\207" ``` The return value of `JS.getResponseText` is overloaded to return something of type `FromJSString result => Maybe result`. There's no instance `FromJSString ByteString` in scope so the next best thing is the `Text` instance. This actually came up in my project and this patch fixed it. --- src/Servant/Client/Internal/JSaddleXhrClient.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Servant/Client/Internal/JSaddleXhrClient.hs b/src/Servant/Client/Internal/JSaddleXhrClient.hs index e219ff2..710ed52 100644 --- a/src/Servant/Client/Internal/JSaddleXhrClient.hs +++ b/src/Servant/Client/Internal/JSaddleXhrClient.hs @@ -282,7 +282,7 @@ toResponse domc xhr = do _ -> inDom $ do statusText <- BS.pack <$> JS.getStatusText xhr headers <- parseHeaders <$> JS.getAllResponseHeaders xhr - responseText <- maybe "" (L.fromStrict . BS.pack) <$> JS.getResponseText xhr -- FIXME: Text/Binary? Performance? Test? + responseText <- maybe "" (L.fromStrict . T.encodeUtf8) <$> JS.getResponseText xhr pure Response { responseStatusCode = mkStatus (fromIntegral status) statusText , responseBody = responseText