-
-
Notifications
You must be signed in to change notification settings - Fork 409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Example for FormUrlEncoded endpoint #236
Comments
Have you looked at If you're making a server, you just need to implement |
Also documentation on |
Here's a minimal example of using {-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE OverloadedStrings #-}
import Control.Applicative
import Data.Aeson
import Data.Monoid
import Servant
type API = "login" :> ReqBody '[FormUrlEncoded] User :> Post '[JSON] Login
data Login = LoggedIn | NotLoggedIn
deriving (Eq, Show)
instance ToJSON Login where
toJSON = toJSON . show
data User = User
{ email :: Text
, password :: Text
} deriving (Eq, Show)
instance FromFormUrlEncoded User where
fromFormUrlEncoded inputs =
User <$> lkp "email" <*> lkp "password"
where lkp input_label = case lookup input_label inputs of
Nothing -> Left $ "label " <> input_label <> " not found"
Just v -> Right v
server :: Server API
server usr =
if email usr == "admin@admin.com" && password usr == "1234"
then return LoggedIn
else return NotLoggedIn Now, as you can see, in the |
Great, thanks! I'll be able to take it from here. |
Feel free to use this issue to ask questions, if you get stuck further down the road. |
I think it'd be very useful to have an example of using |
@jkarni will sure do! |
Can we consider this issue solved/closed? |
Closed, in my opinion. I'll get back later with my solution and open questions regarding the combination of Servant with digestive-functors. |
Yeah, definitely. Just open an issue about this when you manage to make both play well together :) |
Is there an example on how to implement a
FormUrlEncoded
endpoint in Servant?I'm trying to build plain, old HTML forms (using digestive-functors and blaze) and want Servant to handle
GET
andPOST
. For digestive functors I need to build an Env m and I'm looking for a good starting point.Atm I don't even understand what the consequences of the
FormUrlEncoded
-contenttype are, as in here:The text was updated successfully, but these errors were encountered: