on servant-docs-0.11.4 help sign in
44 lines 1.1 kB history raw
1# servant-client
3![servant](https://raw.githubusercontent.com/haskell-servant/servant/master/servant.png)
5This library lets you automatically derive Haskell functions that let you query each endpoint of a *servant* webservice.
7## Example
9``` haskell
10{-# LANGUAGE DataKinds #-}
11{-# LANGUAGE TypeOperators #-}
13import Data.Proxy
14import Data.Text
15import Servant.API
16import Servant.HttpStreams
19type Book = Text
21type MyApi = "books" :> Get '[JSON] [Book] -- GET /books
22 :<|> "books" :> ReqBody '[JSON] Book :> Post '[JSON] Book -- POST /books
24myApi :: Proxy MyApi
25myApi = Proxy
27-- 'client' allows you to produce operations to query an API from a client.
28postNewBook :: Book -> ClientM Book
29getAllBooks :: ClientM [Book]
30(getAllBooks :<|> postNewBook) = client myApi
32-- the IOException happens already in withClientEnvIO
33main' :: IO ()
34main' = do
35 let burl = BaseUrl Http "localhost" 8081 ""
36 withClientEnvIO burl $ \env -> do
37 res <- runClientM getAllBooks env
38 case res of
39 Left err -> putStrLn $ "Error: " ++ show err
40 Right books -> print books
42main :: IO ()
43main = return ()
44```