| Copyright | © 2016–2018 Mark Karpov |
|---|---|
| License | BSD 3 clause |
| Maintainer | Mark Karpov <markkarpov92@gmail.com> |
| Stability | experimental |
| Portability | portable |
| Safe Haskell | None |
| Language | Haskell2010 |
Network.HTTP.Req
Contents
Description
The documentation below is structured in such a way that the most important information is presented first: you learn how to do HTTP requests, how to embed them in any monad you have, and then it gives you details about less-common things you may want to know about. The documentation is written with sufficient coverage of details and examples, and it's designed to be a complete tutorial on its own.
(A modest intro goes here, click on req to start making requests.)
About the library
Req is an easy-to-use, type-safe, expandable, high-level HTTP client library that just works without any fooling around.
What does the phrase “easy-to-use” mean? It means that the library is
designed to be beginner-friendly so it's simple to add to your monad
stack, intuitive to work with, well-documented, and does not get in your
way. Doing HTTP requests is a common task and a Haskell library for this
should be very approachable and clear to beginners, thus certain
compromises were made. For example, one cannot currently modify
ManagerSettings of the default manager because the library always
uses the same implicit global manager for simplicity and maximal
connection sharing. There is a way to use your own manager with different
settings, but it requires a bit more typing.
“Type-safe” means that the library is protective and eliminates certain
classes of errors. For example, we have correct-by-construction Urls,
it's guaranteed that the user does not send the request body when using
methods like GET or OPTIONS, and the amount of implicit assumptions
is minimized by making the user specify his/her intentions in an
explicit form (for example, it's not possible to avoid specifying the
body or method of a request). Authentication methods that assume HTTPS
force the user to use HTTPS at the type level. The library also carefully
hides underlying types from the lower-level http-client package because
those types are not safe enough (for example Request is an instance
of IsString and, if it's malformed, it will blow up at
run-time).
“Expandable” refers to the ability of the library to be expanded without having to resort to ugly hacking. For example, it's possible to define your own HTTP methods, create new ways to construct the body of a request, create new authorization options, perform a request in a different way, and create your own methods to parse and represent a response. As a user extends the library to satisfy his/her special needs, the new solutions will work just like the built-ins. However, all of the common cases are also covered by the library out-of-the-box.
“High-level” means that there are less details to worry about. The
library is a result of my experiences as a Haskell consultant. Working
for several clients, who had very different projects, showed me that the
library should adapt easily to any particular style of writing Haskell
applications. For example, some people prefer throwing exceptions, while
others are concerned with purity. Just define handleHttpException
accordingly when making your monad instance of MonadHttp and it will
play together seamlessly. Finally, the library cuts boilerplate down
considerably, and helps you write concise, easy to read, and maintainable
code.
Using with other libraries
- You won't need the low-level interface of
http-clientmost of the time, but when you do, it's better to do a qualified import, becausehttp-clienthas naming conflicts withreq. - For streaming of large request bodies see the companion package
req-conduit: https://hackage.haskell.org/package/req-conduit.
Lightweight, no risk solution
The library uses the following mature packages under the hood to guarantee you the best experience:
- https://hackage.haskell.org/package/http-client—low level HTTP client used everywhere in Haskell.
- https://hackage.haskell.org/package/http-client-tls—TLS (HTTPS)
support for
http-client.
It's important to note that since we leverage well-known libraries that
the whole Haskell ecosystem uses, there is no risk in using req. The
machinery for performing requests is the same as with http-conduit and
wreq. The only difference is the API.
Synopsis
- req :: (MonadHttp m, HttpMethod method, HttpBody body, HttpResponse response, HttpBodyAllowed (AllowsBody method) (ProvidesBody body)) => method -> Url scheme -> body -> Proxy response -> Option scheme -> m response
- reqBr :: (MonadHttp m, HttpMethod method, HttpBody body, HttpBodyAllowed (AllowsBody method) (ProvidesBody body)) => method -> Url scheme -> body -> Option scheme -> (Response BodyReader -> IO a) -> m a
- req' :: forall m method body scheme a. (MonadHttp m, HttpMethod method, HttpBody body, HttpBodyAllowed (AllowsBody method) (ProvidesBody body)) => method -> Url scheme -> body -> Option scheme -> (Request -> Manager -> m a) -> m a
- withReqManager :: MonadIO m => (Manager -> m a) -> m a
- class MonadIO m => MonadHttp m where
- data HttpConfig = HttpConfig {
- httpConfigProxy :: Maybe Proxy
- httpConfigRedirectCount :: Int
- httpConfigAltManager :: Maybe Manager
- httpConfigCheckResponse :: forall b. Request -> Response b -> ByteString -> Maybe HttpExceptionContent
- httpConfigRetryPolicy :: RetryPolicy
- httpConfigRetryJudge :: forall b. RetryStatus -> Response b -> Bool
- data Req a
- runReq :: MonadIO m => HttpConfig -> Req a -> m a
- data GET = GET
- data POST = POST
- data HEAD = HEAD
- data PUT = PUT
- data DELETE = DELETE
- data TRACE = TRACE
- data CONNECT = CONNECT
- data OPTIONS = OPTIONS
- data PATCH = PATCH
- class HttpMethod a where
- type AllowsBody a :: CanHaveBody
- data Url (scheme :: Scheme)
- http :: Text -> Url Http
- https :: Text -> Url Https
- (/~) :: ToHttpApiData a => Url scheme -> a -> Url scheme
- (/:) :: Url scheme -> Text -> Url scheme
- parseUrlHttp :: ByteString -> Maybe (Url Http, Option scheme)
- parseUrlHttps :: ByteString -> Maybe (Url Https, Option scheme)
- parseUrl :: ByteString -> Maybe (Either (Url Http, Option scheme0) (Url Https, Option scheme1))
- data NoReqBody = NoReqBody
- newtype ReqBodyJson a = ReqBodyJson a
- newtype ReqBodyFile = ReqBodyFile FilePath
- newtype ReqBodyBs = ReqBodyBs ByteString
- newtype ReqBodyLbs = ReqBodyLbs ByteString
- newtype ReqBodyUrlEnc = ReqBodyUrlEnc FormUrlEncodedParam
- data FormUrlEncodedParam
- data ReqBodyMultipart
- reqBodyMultipart :: MonadIO m => [Part] -> m ReqBodyMultipart
- class HttpBody body where
- type family ProvidesBody body :: CanHaveBody where ...
- type family HttpBodyAllowed (allowsBody :: CanHaveBody) (providesBody :: CanHaveBody) :: Constraint where ...
- data Option (scheme :: Scheme)
- (=:) :: (QueryParam param, ToHttpApiData a) => Text -> a -> param
- queryFlag :: QueryParam param => Text -> param
- class QueryParam param where
- header :: ByteString -> ByteString -> Option scheme
- attachHeader :: ByteString -> ByteString -> Request -> Request
- cookieJar :: CookieJar -> Option scheme
- basicAuth :: ByteString -> ByteString -> Option Https
- basicAuthUnsafe :: ByteString -> ByteString -> Option scheme
- basicProxyAuth :: ByteString -> ByteString -> Option scheme
- oAuth1 :: ByteString -> ByteString -> ByteString -> ByteString -> Option scheme
- oAuth2Bearer :: ByteString -> Option Https
- oAuth2Token :: ByteString -> Option Https
- customAuth :: (Request -> IO Request) -> Option scheme
- port :: Int -> Option scheme
- decompress :: (ByteString -> Bool) -> Option scheme
- responseTimeout :: Int -> Option scheme
- httpVersion :: Int -> Int -> Option scheme
- data IgnoreResponse
- ignoreResponse :: Proxy IgnoreResponse
- data JsonResponse a
- jsonResponse :: Proxy (JsonResponse a)
- data BsResponse
- bsResponse :: Proxy BsResponse
- data LbsResponse
- lbsResponse :: Proxy LbsResponse
- responseBody :: HttpResponse response => response -> HttpResponseBody response
- responseStatusCode :: HttpResponse response => response -> Int
- responseStatusMessage :: HttpResponse response => response -> ByteString
- responseHeader :: HttpResponse response => response -> ByteString -> Maybe ByteString
- responseCookieJar :: HttpResponse response => response -> CookieJar
- class HttpResponse response where
- type HttpResponseBody response :: *
- data HttpException
- data CanHaveBody
- data Scheme
Making a request
To make an HTTP request you normally need only one function: req.
Arguments
| :: (MonadHttp m, HttpMethod method, HttpBody body, HttpResponse response, HttpBodyAllowed (AllowsBody method) (ProvidesBody body)) | |
| => method | HTTP method |
| -> Url scheme |
|
| -> body | Body of the request |
| -> Proxy response | A hint how to interpret response |
| -> Option scheme | Collection of optional parameters |
| -> m response | Response |
Make an HTTP request. The function takes 5 arguments, 4 of which
specify required parameters and the final Option argument is a
collection of optional parameters.
Let's go through all the arguments first: req method url body response
options.
method is an HTTP method such as GET or POST. The documentation has
a dedicated section about HTTP methods below.
url is a Url that describes location of resource you want to interact
with.
body is a body option such as NoReqBody or ReqBodyJson. The
tutorial has a section about HTTP bodies, but usage is very
straightforward and should be clear from the examples below.
response is a type hint how to make and interpret response of an HTTP
request. Out-of-the-box it can be the following:
ignoreResponsejsonResponsebsResponse(to get a strictByteString)lbsResponse(to get a lazyByteString)
Finally, options is a Monoid that holds a composite Option for all
other optional settings like query parameters, headers, non-standard port
number, etc. There are quite a few things you can put there, see the
corresponding section in the documentation. If you don't need anything at
all, pass mempty.
Note that if you use req to do all your requests, connection
sharing and reuse is done for you automatically.
See the examples below to get on the speed quickly.
Examples
First, this is a piece of boilerplate that should be in place before you try the examples:
{-# LANGUAGE OverloadedStrings #-}
module Main (main) where
import Control.Monad
import Control.Monad.IO.Class
import Data.Aeson
import Data.Default.Class
import Data.Maybe (fromJust)
import Data.Monoid ((<>))
import Data.Text (Text)
import GHC.Generics
import Network.HTTP.Req
import qualified Data.ByteString.Char8 as BWe will be making requests against the https://httpbin.org service.
Make a GET request, grab 5 random bytes:
main :: IO ()
main = runReq def $ do
let n :: Int
n = 5
bs <- req GET (https "httpbin.org" /: "bytes" /~ n) NoReqBody bsResponse mempty
liftIO $ B.putStrLn (responseBody bs)The same, but now we use a query parameter named "seed" to control
seed of the generator:
main :: IO ()
main = runReq def $ do
let n, seed :: Int
n = 5
seed = 100
bs <- req GET (https "httpbin.org" /: "bytes" /~ n) NoReqBody bsResponse $
"seed" =: seed
liftIO $ B.putStrLn (responseBody bs)POST JSON data and get some info about the POST request:
data MyData = MyData
{ size :: Int
, color :: Text
} deriving (Show, Generic)
instance ToJSON MyData
instance FromJSON MyData
main :: IO ()
main = runReq def $ do
let myData = MyData
{ size = 6
, color = "Green" }
v <- req POST (https "httpbin.org" /: "post") (ReqBodyJson myData) jsonResponse mempty
liftIO $ print (responseBody v :: Value)Sending URL-encoded body:
main :: IO ()
main = runReq def $ do
let params =
"foo" =: ("bar" :: Text) <>
queryFlag "baz"
response <- req POST (https "httpbin.org" /: "post") (ReqBodyUrlEnc params) jsonResponse mempty
liftIO $ print (responseBody response :: Value)Using various optional parameters and URL that is not known in advance:
main :: IO ()
main = runReq def $ do
-- This is an example of what to do when URL is given dynamically. Of
-- course in a real application you may not want to use 'fromJust'.
let (url, options) = fromJust (parseUrlHttps "https://httpbin.org/get?foo=bar")
response <- req GET url NoReqBody jsonResponse $
"from" =: (15 :: Int) <>
"to" =: (67 :: Int) <>
basicAuth "username" "password" <>
options <> -- contains the ?foo=bar part
port 443 -- here you can put any port of course
liftIO $ print (responseBody response :: Value)Arguments
| :: (MonadHttp m, HttpMethod method, HttpBody body, HttpBodyAllowed (AllowsBody method) (ProvidesBody body)) | |
| => method | HTTP method |
| -> Url scheme |
|
| -> body | Body of the request |
| -> Option scheme | Collection of optional parameters |
| -> (Response BodyReader -> IO a) | How to consume response |
| -> m a | Result |
A version of req that does not use one of the predefined instances of
HttpResponse but instead allows the user to consume manually, in a custom way.Response
BodyReader
Since: req-1.0.0
Arguments
| :: (MonadHttp m, HttpMethod method, HttpBody body, HttpBodyAllowed (AllowsBody method) (ProvidesBody body)) | |
| => method | HTTP method |
| -> Url scheme |
|
| -> body | Body of the request |
| -> Option scheme | Collection of optional parameters |
| -> (Request -> Manager -> m a) | How to perform request |
| -> m a | Result |
Mostly like req with respect to its arguments, but accepts a callback
that allows to perform a request in arbitrary fashion.
This function does not perform handling/wrapping exceptions, checking
response (with httpConfigCheckResponse), and retrying. It only prepares
Request and allows you to use it.
Since: req-0.3.0
withReqManager :: MonadIO m => (Manager -> m a) -> m a Source #
Embedding requests into your monad
To use req in your monad, all you need to do is to make the monad an
instance of the MonadHttp type class.
When writing a library, keep your API polymorphic in terms of
MonadHttp, only define instance of MonadHttp in final application.
Another option is to use newtype wrapped monad stack and define
MonadHttp for it. As of version 0.4.0, the Req monad that follows
this strategy is provided out-of-the-box (see below).
class MonadIO m => MonadHttp m where Source #
A type class for monads that support performing HTTP requests.
Typically, you only need to define the handleHttpException method
unless you want to tweak HttpConfig.
Minimal complete definition
Methods
handleHttpException :: HttpException -> m a Source #
This method describes how to deal with HttpException that was
caught by the library. One option is to re-throw it if you are OK with
exceptions, but if you prefer working with something like
MonadError, this is the right place to pass it to
throwError.
getHttpConfig :: m HttpConfig Source #
Return HttpConfig to be used when performing HTTP requests. Default
implementation returns its def value, which is described in the
documentation for the type. Common usage pattern with manually defined
getHttpConfig is to return some hard-coded value, or a value
extracted from MonadReader if a more flexible
approach to configuration is desirable.
Instances
| MonadHttp Req Source # | |
Defined in Network.HTTP.Req Methods handleHttpException :: HttpException -> Req a Source # | |
data HttpConfig Source #
HttpConfig contains general and default settings to be used when
making HTTP requests.
Constructors
| HttpConfig | |
Fields
| |
Instances
| Default HttpConfig Source # | |
Defined in Network.HTTP.Req Methods def :: HttpConfig # | |
A monad that allows to run req in any IO-enabled monad without
having to define new instances.
Since: req-0.4.0
Instances
| Monad Req Source # | |
| Functor Req Source # | |
| Applicative Req Source # | |
| MonadIO Req Source # | |
Defined in Network.HTTP.Req | |
| MonadHttp Req Source # | |
Defined in Network.HTTP.Req Methods handleHttpException :: HttpException -> Req a Source # | |
| MonadBase IO Req Source # | |
Defined in Network.HTTP.Req | |
| MonadBaseControl IO Req Source # | |
| type StM Req a Source # | |
Defined in Network.HTTP.Req | |
Arguments
| :: MonadIO m | |
| => HttpConfig |
|
| -> Req a | Computation to run |
| -> m a |
Run a computation in the Req monad with the given HttpConfig. In
case of exceptional situation an HttpException will be thrown.
Since: req-0.4.0
Request
Method
The package supports all methods as defined by RFC 2616, and PATCH
which is defined by RFC 5789—that should be enough to talk to RESTful
APIs. In some cases, however, you may want to add more methods (e.g. you
work with WebDAV https://en.wikipedia.org/wiki/WebDAV); no need to
compromise on type safety and hack, it only takes a couple of seconds to
define a new method that will works seamlessly, see HttpMethod.
GET method.
Constructors
| GET |
Instances
| HttpMethod GET Source # | |
Defined in Network.HTTP.Req Associated Types type AllowsBody GET :: CanHaveBody Source # Methods httpMethodName :: Proxy GET -> ByteString Source # | |
| type AllowsBody GET Source # | |
Defined in Network.HTTP.Req | |
POST method.
Constructors
| POST |
Instances
| HttpMethod POST Source # | |
Defined in Network.HTTP.Req Associated Types type AllowsBody POST :: CanHaveBody Source # Methods httpMethodName :: Proxy POST -> ByteString Source # | |
| type AllowsBody POST Source # | |
Defined in Network.HTTP.Req | |
HEAD method.
Constructors
| HEAD |
Instances
| HttpMethod HEAD Source # | |
Defined in Network.HTTP.Req Associated Types type AllowsBody HEAD :: CanHaveBody Source # Methods httpMethodName :: Proxy HEAD -> ByteString Source # | |
| type AllowsBody HEAD Source # | |
Defined in Network.HTTP.Req | |
PUT method.
Constructors
| PUT |
Instances
| HttpMethod PUT Source # | |
Defined in Network.HTTP.Req Associated Types type AllowsBody PUT :: CanHaveBody Source # Methods httpMethodName :: Proxy PUT -> ByteString Source # | |
| type AllowsBody PUT Source # | |
Defined in Network.HTTP.Req | |
DELETE method. This data type does not allow having request body with
DELETE requests, as it should be. However some APIs may expect DELETE
requests to have bodies, in that case define your own variation of
DELETE method and allow it to have a body.
Constructors
| DELETE |
Instances
| HttpMethod DELETE Source # | |
Defined in Network.HTTP.Req Associated Types type AllowsBody DELETE :: CanHaveBody Source # Methods httpMethodName :: Proxy DELETE -> ByteString Source # | |
| type AllowsBody DELETE Source # | |
Defined in Network.HTTP.Req | |
TRACE method.
Constructors
| TRACE |
Instances
| HttpMethod TRACE Source # | |
Defined in Network.HTTP.Req Associated Types type AllowsBody TRACE :: CanHaveBody Source # Methods httpMethodName :: Proxy TRACE -> ByteString Source # | |
| type AllowsBody TRACE Source # | |
Defined in Network.HTTP.Req | |
CONNECT method.
Constructors
| CONNECT |
Instances
| HttpMethod CONNECT Source # | |
Defined in Network.HTTP.Req Associated Types type AllowsBody CONNECT :: CanHaveBody Source # Methods httpMethodName :: Proxy CONNECT -> ByteString Source # | |
| type AllowsBody CONNECT Source # | |
Defined in Network.HTTP.Req | |
OPTIONS method.
Constructors
| OPTIONS |
Instances
| HttpMethod OPTIONS Source # | |
Defined in Network.HTTP.Req Associated Types type AllowsBody OPTIONS :: CanHaveBody Source # Methods httpMethodName :: Proxy OPTIONS -> ByteString Source # | |
| type AllowsBody OPTIONS Source # | |
Defined in Network.HTTP.Req | |
PATCH method.
Constructors
| PATCH |
Instances
| HttpMethod PATCH Source # | |
Defined in Network.HTTP.Req Associated Types type AllowsBody PATCH :: CanHaveBody Source # Methods httpMethodName :: Proxy PATCH -> ByteString Source # | |
| type AllowsBody PATCH Source # | |
Defined in Network.HTTP.Req | |
class HttpMethod a where Source #
A type class for types that can be used as an HTTP method. To define a
non-standard method, follow this example that defines COPY:
data COPY = COPY instance HttpMethod COPY where type AllowsBody COPY = 'CanHaveBody httpMethodName Proxy = "COPY"
Minimal complete definition
Associated Types
type AllowsBody a :: CanHaveBody Source #
Type function AllowsBody returns a type of kind CanHaveBody which
tells the rest of the library whether the method can have a body or
not. We use the special type CanHaveBody lifted to the kind level
instead of Bool to get more user-friendly compiler messages.
Instances
URL
We use Urls which are correct by construction, see Url. To build a
Url from a ByteString, use parseUrlHttp, parseUrlHttps, or
generic parseUrl.
data Url (scheme :: Scheme) Source #
Request's Url. Start constructing your Url with http or https
specifying the scheme and host at the same time. Then use the (
and /~)( operators to grow the path one piece at a time. Every single
piece of path will be url(percent)-encoded, so using /:)( and
/~)( is the only way to have forward slashes between path segments.
This approach makes working with dynamic path segments easy and safe. See
examples below how to represent various /:)Urls (make sure the
OverloadedStrings language extension is enabled).
Examples
http "httpbin.org" -- http://httpbin.org
https "httpbin.org" -- https://httpbin.org
https "httpbin.org" /: "encoding" /: "utf8" -- https://httpbin.org/encoding/utf8
https "httpbin.org" /: "foo" /: "bar/baz" -- https://httpbin.org/foo/bar%2Fbaz
https "httpbin.org" /: "bytes" /~ (10 :: Int) -- https://httpbin.org/bytes/10
https "юникод.рф" -- https://%D1%8E%D0%BD%D0%B8%D0%BA%D0%BE%D0%B4.%D1%80%D1%84
Instances
| Eq (Url scheme) Source # | |
| Typeable scheme => Data (Url scheme) Source # | |
Defined in Network.HTTP.Req Methods gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Url scheme -> c (Url scheme) # gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Url scheme) # toConstr :: Url scheme -> Constr # dataTypeOf :: Url scheme -> DataType # dataCast1 :: Typeable t => (forall d. Data d => c (t d)) -> Maybe (c (Url scheme)) # dataCast2 :: Typeable t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Url scheme)) # gmapT :: (forall b. Data b => b -> b) -> Url scheme -> Url scheme # gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Url scheme -> r # gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Url scheme -> r # gmapQ :: (forall d. Data d => d -> u) -> Url scheme -> [u] # gmapQi :: Int -> (forall d. Data d => d -> u) -> Url scheme -> u # gmapM :: Monad m => (forall d. Data d => d -> m d) -> Url scheme -> m (Url scheme) # gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Url scheme -> m (Url scheme) # gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Url scheme -> m (Url scheme) # | |
| Ord (Url scheme) Source # | |
| Show (Url scheme) Source # | |
Defined in Network.HTTP.Req | |