@@ -15,9 +15,30 @@ import Servant.Common.Req
15import Servant.Docs
15import Servant.Docs
16import Servant.Server
16import Servant.Server
18−-- * Request Body support
18+-- | Extract the request body as a value of type @a@.
23+-- > type MyApi = "books" :> ReqBody Book :> Post Book
19data ReqBody a
24data ReqBody a
26+-- | If you use 'ReqBody' in one of the endpoints for your API,
27+-- this automatically requires your server-side handler to be a function
28+-- that takes an argument of the type specified by 'ReqBody'.
29+-- This lets servant worry about extracting it from the request and turning
30+-- it into a value of the type you specify.
32+-- All it asks is for a 'FromJSON' instance.
36+-- > type MyApi = "books" :> ReqBody Book :> Post Book
38+-- > server :: Server MyApi
39+-- > server = postBook
40+-- > where postBook :: Book -> EitherT (Int, String) IO Book
41+-- > postBook book = ...insert into your db...
21instance (FromJSON a, HasServer sublayout)
42instance (FromJSON a, HasServer sublayout)
22 => HasServer (ReqBody a :> sublayout) where
43 => HasServer (ReqBody a :> sublayout) where
@@ -30,6 +51,24 @@ instance (FromJSON a, HasServer sublayout)
30 Nothing -> respond $ failWith InvalidBody
51 Nothing -> respond $ failWith InvalidBody
31 Just v -> route (Proxy :: Proxy sublayout) (subserver v) request respond
52 Just v -> route (Proxy :: Proxy sublayout) (subserver v) request respond
54+-- | If you use a 'ReqBody' in one of your endpoints in your API,
55+-- the corresponding querying function will automatically take
56+-- an additional argument of the type specified by your 'ReqBody'.
57+-- That function will take care of encoding this argument as JSON and
58+-- of using it as the request body.
60+-- All you need is for your type to have a 'ToJSON' instance.
64+-- > type MyApi = "books" :> ReqBody Book :> Post Book
66+-- > myApi :: Proxy MyApi
69+-- > addBook :: Book -> BaseUrl -> EitherT String IO Book
70+-- > addBook = client myApi
71+-- > -- then you can just use "addBook" to query that endpoint
33instance (ToJSON a, HasClient sublayout)
72instance (ToJSON a, HasClient sublayout)
34 => HasClient (ReqBody a :> sublayout) where
73 => HasClient (ReqBody a :> sublayout) where