git.iko.soy/typeable/servant
on master help sign in
haddocks for ReqBody
commitc47f4ae0020e324051322496ea312541072ef066 authorAlp Mestanogullari <alpmestan@gmail.com> dateSat 22 Nov 2014, 18:16 (12 years ago) parente9f2de1
2 files changed+41−2
src/Servant/API/Capture.hs+1−1 src/Servant/API/ReqBody.hs+40−1
src/Servant/API/Capture.hs+1−1
@@ -43,7 +43,7 @@ captured _ = fromText
4343-- >
4444-- > server :: Server MyApi
4545-- > server = getBook
46−-- > where getBook :: Text -> 'EitherT' (Int, String) IO Book
46+-- > where getBook :: Text -> EitherT (Int, String) IO Book
4747-- > getBook isbn = ...
4848instance (KnownSymbol capture, FromText a, HasServer sublayout)
4949 => HasServer (Capture capture a :> sublayout) where
src/Servant/API/ReqBody.hs+40−1
@@ -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