@@ -15,9 +15,30 @@ import Servant.Common.Req
1515import Servant.Docs
1616import Servant.Server
1717
18−-- * Request Body support
18+-- | Extract the request body as a value of type @a@.
19+--
20+-- Example:
21+--
22+-- > -- POST /books
23+-- > type MyApi = "books" :> ReqBody Book :> Post Book
1924data ReqBody a
2025
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.
31+--
32+-- All it asks is for a 'FromJSON' instance.
33+--
34+-- Example:
35+--
36+-- > type MyApi = "books" :> ReqBody Book :> Post Book
37+-- >
38+-- > server :: Server MyApi
39+-- > server = postBook
40+-- > where postBook :: Book -> EitherT (Int, String) IO Book
41+-- > postBook book = ...insert into your db...
2142instance (FromJSON a, HasServer sublayout)
2243 => HasServer (ReqBody a :> sublayout) where
2344
@@ -30,6 +51,24 @@ instance (FromJSON a, HasServer sublayout)
3051 Nothing -> respond $ failWith InvalidBody
3152 Just v -> route (Proxy :: Proxy sublayout) (subserver v) request respond
3253
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.
59+--
60+-- All you need is for your type to have a 'ToJSON' instance.
61+--
62+-- Example:
63+--
64+-- > type MyApi = "books" :> ReqBody Book :> Post Book
65+-- >
66+-- > myApi :: Proxy MyApi
67+-- > myApi = Proxy
68+-- >
69+-- > addBook :: Book -> BaseUrl -> EitherT String IO Book
70+-- > addBook = client myApi
71+-- > -- then you can just use "addBook" to query that endpoint
3372instance (ToJSON a, HasClient sublayout)
3473 => HasClient (ReqBody a :> sublayout) where
3574