How to avoid conversion between different types of strings in haskell using snapframework?

I want to create a decoded result for POST data. In the string conversion, a lot of code is "lost." This makes the code ugly. Any better solutions?

import Codec.Binary.Url (decode') import qualified Data.ByteString.Lazy.Char8 as L (unpack) import qualified Data.ByteString.Char8 as S (unpack, pack) import qualified Data.ByteString.Lazy as LBS (pack) decodeUrlHandler :: Snap() decodeUrlHandler = do body <- readRequestBody (maxBound :: Int64) writeLBS $ LBS.pack $ map (fromMaybe 0) $ decode' $ L.unpack body 

What will be your code for this purpose?

+7
source share
1 answer

Snap automatically decodes the request and makes it available to you through the Request data type. It provides the getRequest and withRequest functions to retrieve the request and a number of other access functions to receive various parts.

There are also convenient features for general operations. To get the POST or GET parameter, see getParam .

Snap gives it to you as ByteString, because this API is at a fairly low level of abstraction, leaving the user with how to handle things like text encoding. I would recommend using a much more efficient Text type instead of String. The Readable class also provides a mechanism to eliminate some patterns of these transformations. The default instances for numbers and text assume UTF8 encoding.

+8
source

All Articles