I am new to Haskell and Servant and am trying to get an official tutorial up and running. I have been trying to get this tutorial working to have a look at it and have been unable to get it working all day.I hate to post a question like this here but I honestly don't know why the code isn't working as I have made no changes to it. I am wondering has anyone else tried to implement this tutorial and has similar issues.
I have seen posts about some tutorials not working with with current servant versions anymore but this tutorial seems to be the most recent for Servant Client.
This is the tutorial http://haskell-servant.readthedocs.io/en/stable/tutorial/Client.html
Here is the code
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TypeOperators #-}
module Client where
import Data.Aeson
import Data.Proxy
import GHC.Generics
import Network.HTTP.Client (newManager, defaultManagerSettings)
import Servant.API
import Servant.Client
data Position = Position
{ x :: Int
, y :: Int
} deriving (Show, Generic)
instance FromJSON Position
newtype HelloMessage = HelloMessage { msg :: String }
deriving (Show, Generic)
instance FromJSON HelloMessage
data ClientInfo = ClientInfo
{ clientName :: String
, clientEmail :: String
, clientAge :: Int
, clientInterestedIn :: [String]
} deriving Generic
instance ToJSON ClientInfo
data Email = Email
{ from :: String
, to :: String
, subject :: String
, body :: String
} deriving (Show, Generic)
instance FromJSON Email
type API = "position" :> Capture "x" Int :> Capture "y" Int :> Get '[JSON] Position
:<|> "hello" :> QueryParam "name" String :> Get '[JSON] HelloMessage
:<|> "marketing" :> ReqBody '[JSON] ClientInfo :> Post '[JSON] Email
position :: Int -> Int -> ClientM Position
hello :: Maybe String -> ClientM HelloMessage
marketing :: ClientInfo -> ClientM Email
api :: Proxy API
api = Proxy
position :<|> hello :<|> marketing = client api
queries :: ClientM (Position, HelloMessage, Email)
queries = do
pos <- position 10 10
message <- hello (Just "servant")
em <- marketing (ClientInfo "Alp" "[email protected]" 26 ["haskell", "mathematics"])
return (pos, message, em)
run :: IO ()
run = do
manager <- newManager defaultManagerSettings
res <- runClientM queries (ClientEnv manager (BaseUrl Http "localhost" 8081 ""))
case res of
Left err -> putStrLn $ "Error: " ++ show err
Right (pos, message, em) -> do
print pos
print message
print em
Here are the errors I am getting
Couldn't match type ‘http-client-0.4.31.2:Network.HTTP.Client.Types.Manager -> BaseUrl -> ClientM Position’ with ‘Control.Monad.Trans.Except.ExceptT ServantError IO Position’ Expected type: Int -> Int -> ClientM Position Actual type: Int -> Int -> http-client-0.4.31.2:Network.HTTP.Client.Types.Manager -> BaseUrl -> ClientM Position • When checking that the inferred type position :: Int -> Int -> http-client-0.4.31.2:Network.HTTP.Client.Types.Manager -> BaseUrl -> ClientM Position is as general as its signature position :: Int -> Int -> ClientM Position Variable not in scope: runClientM :: ClientM (Position, HelloMessage, Email) -> t0 -> IO (Either a0 (a1, a2, a3)) Data constructor not in scope: ClientEnv :: http-client-0.4.31.2:Network.HTTP.Client.Types.Manager -> BaseUrl -> t0
Variable not in scope: runClientM.runClientM, which is exported byServant.Client, was introduced in Servant 0.9 (cf. the changelog and the Haddock indexes for servant-client 0.8.1 and servant-client 0.9.1.1). Are you sure that you are using the latest version of Servant? - duplodeservant-client >= 0.9to the .cabal file is the right thing to do -- now cabal-install demands the version you actually need. The new error message tells you that you currently have 0.8.1, and need to install 0.9.1.1.cabal install servantshould do the trick. (Note: What I have just said assumes that you are using just cabal-install, and not stack, to manage your packages and project. If you never heard of stack, you can safely ignore this note.) - duplode