How to create a JSON Rest API using Happstack? JSON body?

I am trying to create a JSON REST api using Happstack. This should enable POSTS with a JSON enclosure. How can i do this? All the functions in the happstack API seem to look based on the parameter name. He thinks the body is always encoded in url.

If this is not possible with Happstack, what structure should I use?

+6
source share
1 answer

Well, that’s what I understood.

{-# LANGUAGE OverloadedStrings, DeriveDataTypeable #-} import qualified Data.ByteString.Lazy.Char8 as L import Happstack.Server import Happstack.Server.Types import Control.Monad.IO.Class (liftIO) import Data.Data (Data, Typeable) -- easiest to serialize/deserialize objects data Unit = Unit { x :: Int, y :: Int } deriving (Show, Eq, Data, Typeable) -- put this function in a library somewhere getBody :: ServerPart L.ByteString getBody = do req <- askRq body <- liftIO $ takeRequestBody req case body of Just rqbody -> return . unBody $ rqbody Nothing -> return "" myRoute :: ServerPart Response myRoute = do body <- getBody -- it a ByteString let unit = fromJust $ A.decode body :: Unit -- how to parse json ok $ toResponse $ A.encode unit -- how to send json back. 
+6
source

Source: https://habr.com/ru/post/905999/


All Articles