Skip to content

Instantly share code, notes, and snippets.

@jhrcek
Last active April 5, 2019 05:48
Show Gist options
  • Save jhrcek/8652c059c1d2925957b4805e923a36b9 to your computer and use it in GitHub Desktop.
Save jhrcek/8652c059c1d2925957b4805e923a36b9 to your computer and use it in GitHub Desktop.
servant-client basic auth example, calling httpbin.org
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeOperators #-}
module Main where
import Data.Aeson (FromJSON, ToJSON)
import Data.Proxy (Proxy (Proxy))
import Data.Text (Text)
import GHC.Generics
import Network.HTTP.Client (defaultManagerSettings, newManager)
import Servant.API
import Servant.Client (BaseUrl (BaseUrl), ClientM, Scheme (Http), client,
mkClientEnv, runClientM)
main :: IO ()
main = do
mgr <- newManager defaultManagerSettings
runClientM (basicAuthClient u) (mkClientEnv mgr (BaseUrl Http "httpbin.org" 80 "basic-auth"))
>>= print
where u = BasicAuthData "john" "123"
type API = BasicAuth "Realm doesnt matter" () :> "john" :> "123" :> Get '[JSON] User
api :: Proxy API
api = Proxy
data User = User
{ authenticated :: Bool
, user :: Text
} deriving (Show, Eq, Generic)
instance FromJSON User where
instance ToJSON User where
basicAuthClient :: BasicAuthData -> ClientM User
basicAuthClient = client api
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment