on master help sign in
Support setting Accept headers from ‘HttpResponse’ instances
f598f8cGeorgi Lyubenov
CHANGELOG.md+5−1
@@ -1,7 +1,11 @@
1−## Unreleased
1+## Req 2.2.0
2
2
3* Dropped support for GHC 7.10.
3* Dropped support for GHC 7.10.
4
4
5+* Added the new `acceptHeader` method to the `HttpResponse` type class.
6+ Notably, the `jsonResponse` method now sets `"Accept"` header to
7+ `"application/json"`.
8+
5## Req 2.0.1
9## Req 2.0.1
6
10
7* Fixed the `httpbin` tests (they changed something on the server again).
11* Fixed the `httpbin` tests (they changed something on the server again).
Network/HTTP/Req.hs+20−3
@@ -102,7 +102,6 @@
102{-# LANGUAGE ScopedTypeVariables #-}
102{-# LANGUAGE ScopedTypeVariables #-}
103{-# LANGUAGE TypeFamilies #-}
103{-# LANGUAGE TypeFamilies #-}
104{-# LANGUAGE UndecidableInstances #-}
104{-# LANGUAGE UndecidableInstances #-}
105−{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
106
105
107module Network.HTTP.Req
106module Network.HTTP.Req
108 ( -- * Making a request
107 ( -- * Making a request
@@ -391,8 +390,13 @@ req
391 -> Proxy response -- ^ A hint how to interpret response
390 -> Proxy response -- ^ A hint how to interpret response
392 -> Option scheme -- ^ Collection of optional parameters
391 -> Option scheme -- ^ Collection of optional parameters
393 -> m response -- ^ Response
392 -> m response -- ^ Response
394−req method url body Proxy options =
393+req method url body responseProxy options =
395− reqBr method url body options getHttpResponse
394+ reqBr method url body (options <> extraOptions) getHttpResponse
395+ where
396+ extraOptions =
397+ case acceptHeader responseProxy of
398+ Nothing -> mempty
399+ Just accept -> header "Accept" accept
396
400
397-- | A version of 'req' that does not use one of the predefined instances of
401-- | A version of 'req' that does not use one of the predefined instances of
398-- 'HttpResponse' but instead allows the user to consume @'L.Response'
402-- 'HttpResponse' but instead allows the user to consume @'L.Response'
@@ -1440,6 +1444,7 @@ instance FromJSON a => HttpResponse (JsonResponse a) where
1440 case A.eitherDecode (BL.fromChunks chunks) of
1444 case A.eitherDecode (BL.fromChunks chunks) of
1441 Left e -> throwIO (JsonHttpException e)
1445 Left e -> throwIO (JsonHttpException e)
1442 Right x -> return $ JsonResponse (x <$ r)
1446 Right x -> return $ JsonResponse (x <$ r)
1447+ acceptHeader Proxy = Just "application/json"
1443
1448
1444-- | Use this as the fourth argument of 'req' to specify that you want it to
1449-- | Use this as the fourth argument of 'req' to specify that you want it to
1445-- return the 'JsonResponse' interpretation.
1450-- return the 'JsonResponse' interpretation.
@@ -1637,6 +1642,18 @@ class HttpResponse response where
1637 -> IO response
1642 -> IO response
1638 -- ^ The final result
1643 -- ^ The final result
1639
1644
1645+ -- | The value of @\"Accept\"@ header. This is useful, for example, if a
1646+ -- website supports both @XML@ and @JSON@ responses, and decides what to
1647+ -- reply with based on what @Accept@ headers you have sent.
1648+ --
1649+ -- __Note__: manually specified 'Options' that set the @\"Accept\"@ header
1650+ -- will take precedence.
1651+ --
1652+ -- @since 2.2.0
1653+
1654+ acceptHeader :: Proxy response -> Maybe ByteString
1655+ acceptHeader Proxy = Nothing
1656+
1640----------------------------------------------------------------------------
1657----------------------------------------------------------------------------
1641-- Other
1658-- Other
1642
1659