Skip to content

Commit bc04d12

Browse files
committed
Allow more flexbility in setting the request body.
Rather than hard-coding the `RequestBodyLBS` constructor and be limited to lazy bytestrings, the new function `setReqBody` just takes any value of type `RequestBody`. The old function `setRQBody` has been renamed to `setReqBodyLBS`. The old name is still available, but deprecated. The change has the advantage the we can define new servant API combinators that use streaming request bodies such as for example constructed by the `streamFile` function in http-client. The behaviour for the existing `ReqBody` API combinator is unaffected by this change.
1 parent 875f592 commit bc04d12

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

servant-client/servant-client.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: servant-client
2-
version: 0.9.1.1
2+
version: 0.9.2.0
33
synopsis: automatical derivation of querying functions for servant webservices
44
description:
55
This library lets you derive automatically Haskell functions that

servant-client/src/Servant/Client.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ instance (MimeRender ct a, HasClient api)
406406
clientWithRoute Proxy req body =
407407
clientWithRoute (Proxy :: Proxy api)
408408
(let ctProxy = Proxy :: Proxy ct
409-
in setRQBody (mimeRender ctProxy body)
409+
in setReqBodyLBS (mimeRender ctProxy body)
410410
-- We use first contentType from the Accept list
411411
(contentType ctProxy)
412412
req

servant-client/src/Servant/Common/Req.hs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ instance Exception ServantError
8787
data Req = Req
8888
{ reqPath :: String
8989
, qs :: QueryText
90-
, reqBody :: Maybe (ByteString, MediaType)
90+
, reqBody :: Maybe (RequestBody, MediaType)
9191
, reqAccept :: [MediaType]
9292
, headers :: [(String, Text)]
9393
}
@@ -112,8 +112,31 @@ addHeader name val req = req { headers = headers req
112112
++ [(name, decodeUtf8 (toHeader val))]
113113
}
114114

115+
-- | Set body and media type of the request being constructed.
116+
--
117+
-- The body is set to the given bytestring using the 'RequestBodyLBS'
118+
-- constructor.
119+
--
120+
{-# DEPRECATED setRQBody "Use setReqBodyLBS instead" #-}
115121
setRQBody :: ByteString -> MediaType -> Req -> Req
116-
setRQBody b t req = req { reqBody = Just (b, t) }
122+
setRQBody = setReqBodyLBS
123+
124+
-- | Set body and media type of the request being constructed.
125+
--
126+
-- The body is set to the given bytestring using the 'RequestBodyLBS'
127+
-- constructor.
128+
--
129+
-- @since 0.9.2.0
130+
--
131+
setReqBodyLBS :: ByteString -> MediaType -> Req -> Req
132+
setReqBodyLBS b t req = req { reqBody = Just (RequestBodyLBS b, t) }
133+
134+
-- | Set body and media type of the request being constructed.
135+
--
136+
-- @since 0.9.2.0
137+
--
138+
setReqBody :: RequestBody -> MediaType -> Req -> Req
139+
setReqBody b t req = req { reqBody = Just (b, t) }
117140

118141
reqToRequest :: (Functor m, MonadThrow m) => Req -> BaseUrl -> m Request
119142
reqToRequest req (BaseUrl reqScheme reqHost reqPort path) =
@@ -132,7 +155,7 @@ reqToRequest req (BaseUrl reqScheme reqHost reqPort path) =
132155

133156
setrqb r = case reqBody req of
134157
Nothing -> r
135-
Just (b,t) -> r { requestBody = RequestBodyLBS b
158+
Just (b,t) -> r { requestBody = b
136159
, requestHeaders = requestHeaders r
137160
++ [(hContentType, cs . show $ t)] }
138161
setQS = setQueryString $ queryTextToQuery (qs req)

0 commit comments

Comments
 (0)