http-client-0.5.6: An HTTP client engine

Safe Haskell | None
Language | Haskell2010

Network.HTTP.Client

Contents

- Performing requests
  - Tracking redirect history
- Connection manager
  - Connection manager settings
    - Manager proxy settings
    - Response timeouts
    - Helpers
- Request
  - Request type and fields
  - Request body
- Response
  - Response body
- Advanced connection creation
- Misc
- Cookies

Description

# Simpler API

The API below is rather low-level. The `Network.HTTP.Simple` module (from the `http-conduit` package) provides a higher-level API with built-in support for things like JSON request and response bodies. For most users, this will be an easier place to start. You can read the tutorial at:

https://haskell-lang.org/library/http-client

# Lower-level API

This is the main entry point for using http-client. Used by itself, this module provides low-level access for streaming request and response bodies, and only non-secure HTTP connections. Helper packages such as http-conduit provide higher level streaming approaches, while other helper packages like http-client-tls provide secure connections.

There are three core components to be understood here: requests, responses, and managers. A `Manager` keeps track of open connections to various hosts, and when requested, will provide either an existing open connection or create a new connection on demand. A `Manager` also automatically reaps connections which have been unused for a certain period of time. A `Manager` allows for more efficient HTTP usage by allowing for keep-alive connections. Secure HTTP connections can be allowed by modifying the settings used for creating a manager. The simplest way to create a `Manager` is with:

    newManager defaultManagerSettings

While generally speaking it is a good idea to share a single `Manager` throughout your application, there are cases where it makes more sense to create and destroy `Manager`s more frequently. As an example, if you have an application which will make a large number of requests to different hosts, and will never make more than one connection to a single host, then sharing a `Manager` will result in idle connections being kept open longer than necessary. In such a situation, it makes sense to use `withManager` around each new request, to avoid running out of file descriptors. (Note that the `managerIdleConnectionCount` setting mitigates the risk of leaking too many file descriptors.)

The next core component is a `Request`, which represents a single HTTP request to be sent to a specific server. `Request`s allow for many settings to control exact how they function, but usually the simplest approach for creating a `Request` is to use `parseRequest`.

Finally, a `Response` is the result of sending a single `Request` to a server, over a connection which was acquired from a `Manager`. Note that you must close the response when you're done with it to ensure that the connection is recycled to the `Manager` to either be used by another request, or to be reaped. Usage of `withResponse` will ensure that this happens automatically.

Helper packages may provide replacements for various recommendations listed above. For example, if using http-client-tls, instead of using `defaultManagerSettings`, you would want to use `tlsManagerSettings`. Be sure to read the relevant helper library documentation for more information.

A note on exceptions: for the most part, all actions that perform I/O should be assumed to throw an `HttpException` in the event of some problem, and all pure functions will be total. For example, `withResponse`, `httpLbs`, and `BodyReader` can all throw exceptions. Functions like `responseStatus` and `applyBasicAuth` are guaranteed to be total (or there's a bug in the library).

One thing to be cautioned about: the type of `parseRequest` allows it to work in different monads. If used in the `IO` monad, it will throw an exception in the case of an invalid URI. In addition, if you leverage the `IsString` instance of the `Request` value via `OverloadedStrings`, an invalid URI will result in a partial value. Caveat emptor!

Synopsis

- withResponse :: Request -> Manager -> (Response BodyReader -> IO a) -> IO a
- httpLbs :: Request -> Manager -> IO (Response ByteString)
- httpNoBody :: Request -> Manager -> IO (Response ())
- responseOpen :: Request -> Manager -> IO (Response BodyReader)
- responseClose :: Response a -> IO ()
- withResponseHistory :: Request -> Manager -> (HistoriedResponse BodyReader -> IO a) -> IO a
- responseOpenHistory :: Request -> Manager -> IO (HistoriedResponse BodyReader)
- data HistoriedResponse body
- hrRedirects :: HistoriedResponse body -> [(Request, Response ByteString)]
- hrFinalRequest :: HistoriedResponse body -> Request
- hrFinalResponse :: HistoriedResponse body -> Response body
- data Manager
- newManager :: ManagerSettings -> IO Manager
- closeManager :: Manager -> IO ()
- withManager :: ManagerSettings -> (Manager -> IO a) -> IO a
- class HasHttpManager a where
- data ManagerSettings
- defaultManagerSettings :: ManagerSettings
- managerConnCount :: ManagerSettings -> Int
- managerRawConnection :: ManagerSettings -> IO (Maybe HostAddress -> String -> Int -> IO Connection)
- managerTlsConnection :: ManagerSettings -> IO (Maybe HostAddress -> String -> Int -> IO Connection)
- managerResponseTimeout :: ManagerSettings -> ResponseTimeout
- managerRetryableException :: ManagerSettings -> SomeException -> Bool
- managerWrapException :: ManagerSettings -> forall a. Request -> IO a -> IO a
- managerIdleConnectionCount :: ManagerSettings -> Int
- managerModifyRequest :: ManagerSettings -> Request -> IO Request
- managerModifyResponse :: ManagerSettings -> Response BodyReader -> IO (Response BodyReader)
- managerSetProxy :: ProxyOverride -> ManagerSettings -> ManagerSettings
- managerSetInsecureProxy :: ProxyOverride -> ManagerSettings -> ManagerSettings
- managerSetSecureProxy :: ProxyOverride -> ManagerSettings -> ManagerSettings
- data ProxyOverride
- proxyFromRequest :: ProxyOverride
- noProxy :: ProxyOverride
- useProxy :: Proxy -> ProxyOverride
- proxyEnvironment :: Maybe Proxy -> ProxyOverride
- proxyEnvironmentNamed :: Text -> Maybe Proxy -> ProxyOverride
- defaultProxy :: ProxyOverride
- data ResponseTimeout
- responseTimeoutMicro :: Int -> ResponseTimeout
- responseTimeoutNone :: ResponseTimeout
- responseTimeoutDefault :: ResponseTimeout
- rawConnectionModifySocket :: (Socket -> IO ()) -> IO (Maybe HostAddress -> String -> Int -> IO Connection)
- rawConnectionModifySocketSize :: (Socket -> IO ()) -> IO (Int -> Maybe HostAddress -> String -> Int -> IO Connection)
- parseUrl :: MonadThrow m => String -> m Request
- parseUrlThrow :: MonadThrow m => String -> m Request
- parseRequest :: MonadThrow m => String -> m Request
- parseRequest_ :: String -> Request
- defaultRequest :: Request
- applyBasicAuth :: ByteString -> ByteString -> Request -> Request
- urlEncodedBody :: [(ByteString, ByteString)] -> Request -> Request
- getUri :: Request -> URI
- setRequestIgnoreStatus :: Request -> Request
- setQueryString :: [(ByteString, Maybe ByteString)] -> Request -> Request
- data Request
- method :: Request -> Method
- secure :: Request -> Bool
- host :: Request -> ByteString
- port :: Request -> Int
- path :: Request -> ByteString
- queryString :: Request -> ByteString
- requestHeaders :: Request -> RequestHeaders
- requestBody :: Request -> RequestBody
- proxy :: Request -> Maybe Proxy
- applyBasicProxyAuth :: ByteString -> ByteString -> Request -> Request
- decompress :: Request -> ByteString -> Bool
- redirectCount :: Request -> Int
- checkResponse :: Request -> Request -> Response BodyReader -> IO ()
- responseTimeout :: Request -> ResponseTimeout
- cookieJar :: Request -> Maybe CookieJar
- requestVersion :: Request -> HttpVersion
- data RequestBody
  - = RequestBodyLBS ByteString
  - | RequestBodyBS ByteString
  - | RequestBodyBuilder Int64 Builder
  - | RequestBodyStream Int64 (GivesPopper ())
  - | RequestBodyStreamChunked (GivesPopper ())
  - | RequestBodyIO (IO RequestBody)
- type Popper = IO ByteString
- type NeedsPopper a = Popper -> IO a
- type GivesPopper a = NeedsPopper a -> IO a
- streamFile :: FilePath -> IO RequestBody
- observedStreamFile :: (StreamFileStatus -> IO ()) -> FilePath -> IO RequestBody
- data StreamFileStatus = StreamFileStatus {
  - fileSize :: Int64
  - readSoFar :: Int64
  - thisChunkSize :: Int
  }
- data Response body
- responseStatus :: Response body -> Status
- responseVersion :: Response body -> HttpVersion
- responseHeaders :: Response body -> ResponseHeaders
- responseBody :: Response body -> body
- responseCookieJar :: Response body -> CookieJar
- type BodyReader = IO ByteString
- brRead :: BodyReader -> IO ByteString
- brReadSome :: BodyReader -> Int -> IO ByteString
- brConsume :: BodyReader -> IO [ByteString]
- makeConnection :: IO ByteString -> (ByteString -> IO ()) -> IO () -> IO Connection
- socketConnection :: Socket -> Int -> IO Connection
- data HttpException
  - = HttpExceptionRequest Request HttpExceptionContent
  - | InvalidUrlException String String
- data HttpExceptionContent
  - = StatusCodeException (Response ()) ByteString
  - | TooManyRedirects [Response ByteString]
  - | OverlongHeaders
  - | ResponseTimeout
  - | ConnectionTimeout
  - | ConnectionFailure SomeException
  - | InvalidStatusLine ByteString
  - | InvalidHeader ByteString
  - | InternalException SomeException
  - | ProxyConnectException ByteString Int Status
  - | NoResponseDataReceived
  - | TlsNotSupported
  - | WrongRequestBodyStreamSize Word64 Word64
  - | ResponseBodyTooShort Word64 Word64
  - | InvalidChunkHeaders
  - | IncompleteHeaders
  - | InvalidDestinationHost ByteString
  - | HttpZlibException ZlibException
  - | InvalidProxyEnvironmentVariable Text Text
  - | ConnectionClosed
- data Cookie = Cookie {
  - cookie_name :: ByteString
  - cookie_value :: ByteString
  - cookie_expiry_time :: UTCTime
  - cookie_domain :: ByteString
  - cookie_path :: ByteString
  - cookie_creation_time :: UTCTime
  - cookie_last_access_time :: UTCTime
  - cookie_persistent :: Bool
  - cookie_host_only :: Bool
  - cookie_secure_only :: Bool
  - cookie_http_only :: Bool
  }
- data CookieJar
- data Proxy = Proxy {
  - proxyHost :: ByteString
  - proxyPort :: Int
  }
- updateCookieJar :: Response a -> Request -> UTCTime -> CookieJar -> (CookieJar, Response a)
- receiveSetCookie :: SetCookie -> Request -> UTCTime -> Bool -> CookieJar -> CookieJar
- generateCookie :: SetCookie -> Request -> UTCTime -> Bool -> Maybe Cookie
- insertCheckedCookie :: Cookie -> CookieJar -> Bool -> CookieJar
- insertCookiesIntoRequest :: Request -> CookieJar -> UTCTime -> (Request, CookieJar)
- computeCookieString :: Request -> CookieJar -> UTCTime -> Bool -> (ByteString, CookieJar)
- evictExpiredCookies :: CookieJar -> UTCTime -> CookieJar
- createCookieJar :: [Cookie] -> CookieJar
- destroyCookieJar :: CookieJar -> [Cookie]
- pathMatches :: ByteString -> ByteString -> Bool
- removeExistingCookieFromCookieJar :: Cookie -> CookieJar -> (Maybe Cookie, CookieJar)
- domainMatches :: ByteString -> ByteString -> Bool
- isIpAddress :: ByteString -> Bool
- defaultPath :: Request -> ByteString

# Documentation

# Example Usage

### Making a GET request

    import Network.HTTP.Client
    import Network.HTTP.Types.Status (statusCode)

    main :: IO ()
    main = do
      manager <- newManager defaultManagerSettings

      request <- parseRequest "http://httpbin.org/post"
      response <- httpLbs request manager

      putStrLn $ "The status code was: " ++ (show $ statusCode $ responseStatus response)
      print $ responseBody response

### Posting JSON to a server

    {-# LANGUAGE OverloadedStrings #-}
    import Network.HTTP.Client
    import Network.HTTP.Types.Status (statusCode)
    import Data.Aeson (object, (.=), encode)

    main :: IO ()
    main = do
      manager <- newManager defaultManagerSettings

      -- Create the request
      let requestObject = object ["name" .= "Michael", "age" .= 30]
      initialRequest <- parseRequest "http://httpbin.org/post"
      let request = initialRequest { method = "POST", requestBody = RequestBodyLBS $ encode requestObject }

      response <- httpLbs request manager
      putStrLn $ "The status code was: " ++ (show $ statusCode $ responseStatus response)
      print $ responseBody response

# Performing requests

withResponse :: Request -> Manager -> (Response BodyReader -> IO a) -> IO a #

Perform a `Request` using a connection acquired from the given `Manager`, and then provide the `Response` to the given function. This function is fully exception safe, guaranteeing that the response will be closed when the inner function exits. It is defined as:

    withResponse req man f = bracket (responseOpen req man) responseClose f

It is recommended that you use this function in place of explicit calls to `responseOpen` and `responseClose`.

You will need to use functions such as `brRead` to consume the response body.

Since 0.1.0

httpLbs :: Request -> Manager -> IO (Response ByteString) #

A convenience wrapper around `withResponse` which reads in the entire response body and immediately closes the connection. Note that this function performs fully strict I/O, and only uses a lazy ByteString in its response for memory efficiency. If you are anticipating a large response body, you are encouraged to use `withResponse` and `brRead` instead.

Since 0.1.0

httpNoBody :: Request -> Manager -> IO (Response ()) #

A convenient wrapper around `withResponse` which ignores the response body. This is useful, for example, when performing a HEAD request.

Since 0.3.2

responseOpen :: Request -> Manager -> IO (Response BodyReader) #

The most low-level function for initiating an HTTP request.

The first argument to this function gives a full specification on the request: the host to connect to, whether to use SSL, headers, etc. Please see `Request` for full details. The second argument specifies which `Manager` should be used.

This function then returns a `Response` with a `BodyReader`. The `Response` contains the status code and headers that were sent back to us, and the `BodyReader` contains the body of the request. Note that this `BodyReader` allows you to have fully interleaved IO actions during your HTTP download, making it possible to download very large responses in constant memory.

An important note: the response body returned by this function represents a live HTTP connection. As such, if you do not use the response body, an open socket will be retained indefinitely. You must be certain to call `responseClose` on this response to free up resources.

This function automatically performs any necessary redirects, as specified by the `redirectCount` setting.

When implementing a (reverse) proxy using this function or relating functions, it's wise to remove Transfer-Encoding:, Content-Length:, Content-Encoding: and Accept-Encoding: from request and response headers to be relayed.

Since 0.1.0

responseClose :: Response a -> IO () #

Close any open resources associated with the given `Response`. In general, this will either close an active `Connection` or return it to the `Manager` to be reused.

Since 0.1.0

## Tracking redirect history

withResponseHistory :: Request -> Manager -> (HistoriedResponse BodyReader -> IO a) -> IO a #

A variant of `withResponse` which keeps a history of all redirects performed in the interim, together with the first 1024 bytes of their response bodies.

Since 0.4.1

responseOpenHistory :: Request -> Manager -> IO (HistoriedResponse BodyReader) #

A variant of `responseOpen` which keeps a history of all redirects performed in the interim, together with the first 1024 bytes of their response bodies.

Since 0.4.1

data HistoriedResponse body #

A datatype holding information on redirected requests and the final response.

Since 0.4.1

Instances

Functor HistoriedResponse # |
Methods fmap :: (a -> b) -> HistoriedResponse a -> HistoriedResponse b # (<$) :: a -> HistoriedResponse b -> HistoriedResponse a #
Foldable HistoriedResponse # |
Methods fold :: Monoid m => HistoriedResponse m -> m # foldMap :: Monoid m => (a -> m) -> HistoriedResponse a -> m # foldr :: (a -> b -> b) -> b -> HistoriedResponse a -> b # foldr' :: (a -> b -> b) -> b -> HistoriedResponse a -> b # foldl :: (b -> a -> b) -> b -> HistoriedResponse a -> b # foldl' :: (b -> a -> b) -> b -> HistoriedResponse a -> b # foldr1 :: (a -> a -> a) -> HistoriedResponse a -> a # foldl1 :: (a -> a -> a) -> HistoriedResponse a -> a # toList :: HistoriedResponse a -> [a] # null :: HistoriedResponse a -> Bool # length :: HistoriedResponse a -> Int # elem :: Eq a => a -> HistoriedResponse a -> Bool # maximum :: Ord a => HistoriedResponse a -> a # minimum :: Ord a => HistoriedResponse a -> a # sum :: Num a => HistoriedResponse a -> a # product :: Num a => HistoriedResponse a -> a #
Traversable HistoriedResponse # |
Methods traverse :: Applicative f => (a -> f b) -> HistoriedResponse a -> f (HistoriedResponse b) # sequenceA :: Applicative f => HistoriedResponse (f a) -> f (HistoriedResponse a) # mapM :: Monad m => (a -> m b) -> HistoriedResponse a -> m (HistoriedResponse b) # sequence :: Monad m => HistoriedResponse (m a) -> m (HistoriedResponse a) #
Show body => Show (HistoriedResponse body) # |
Methods showsPrec :: Int -> HistoriedResponse body -> ShowS # show :: HistoriedResponse body -> String # showList :: [HistoriedResponse body] -> ShowS #
Generic (HistoriedResponse body) # |
Associated Types type Rep (HistoriedResponse body) :: * -> * # Methods from :: HistoriedResponse body -> Rep (HistoriedResponse body) x # to :: Rep (HistoriedResponse body) x -> HistoriedResponse body #
type Rep (HistoriedResponse body) # |
type Rep (HistoriedResponse body) = D1 (MetaData "HistoriedResponse" "Network.HTTP.Client" "http-client-0.5.6-Auo5TBlvhCDIsl575hjQy2" False) (C1 (MetaCons "HistoriedResponse" PrefixI True) ((:*:) (S1 (MetaSel (Just Symbol "hrRedirects") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 [(Request, Response ByteString)])) ((:*:) (S1 (MetaSel (Just Symbol "hrFinalRequest") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Request)) (S1 (MetaSel (Just Symbol "hrFinalResponse") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 (Response body))))))

hrRedirects :: HistoriedResponse body -> [(Request, Response ByteString)] #

Requests which resulted in a redirect, together with their responses. The response contains the first 1024 bytes of the body.

Since 0.4.1

hrFinalRequest :: HistoriedResponse body -> Request #

The final request performed.

Since 0.4.1

hrFinalResponse :: HistoriedResponse body -> Response body #

The response from the final request.

Since 0.4.1

# Connection manager

data Manager #

Keeps track of open connections for keep-alive.

If possible, you should share a single `Manager` between multiple threads and requests.

Since 0.1.0

Instances

HasHttpManager Manager # |
Methods getHttpManager :: Manager -> Manager #

newManager :: ManagerSettings -> IO Manager #

Create a `Manager`. The `Manager` will be shut down automatically via garbage collection.

Creating a new `Manager` is a relatively expensive operation, you are advised to share a single `Manager` between requests instead.

The first argument to this function is often `defaultManagerSettings`, though add-on libraries may provide a recommended replacement.

Since 0.1.0

closeManager :: Manager -> IO () #

Deprecated: Manager will be closed for you automatically when no longer in use

Close all connections in a `Manager`.

Note that this doesn't affect currently in-flight connections, meaning you can safely use it without hurting any queries you may have concurrently running.

Since 0.1.0

withManager :: ManagerSettings -> (Manager -> IO a) -> IO a #

Deprecated: Use newManager instead

Create, use and close a `Manager`.

Since 0.2.1

class HasHttpManager a where #

Minimal complete definition

getHttpManager

Methods

getHttpManager :: a -> Manager #

Instances

HasHttpManager Manager # |
Methods getHttpManager :: Manager -> Manager #

## Connection manager settings

data ManagerSettings #

Settings for a `Manager`. Please use the `defaultManagerSettings` function and then modify individual settings. For more information, see http://www.yesodweb.com/book/settings-types.

Since 0.1.0

defaultManagerSettings :: ManagerSettings #

Default value for `ManagerSettings`.

Note that this value does not have support for SSL/TLS. If you need to make any https connections, please use the http-client-tls package, which provides a `tlsManagerSettings` value.

Since 0.1.0

managerConnCount :: ManagerSettings -> Int #

Number of connections to a single host to keep alive. Default: 10.

Since 0.1.0

managerRawConnection :: ManagerSettings -> IO (Maybe HostAddress -> String -> Int -> IO Connection) #

Create an insecure connection.

Since 0.1.0

managerTlsConnection :: ManagerSettings -> IO (Maybe HostAddress -> String -> Int -> IO Connection) #

Create a TLS connection. Default behavior: throw an exception that TLS is not supported.

Since 0.1.0

managerResponseTimeout :: ManagerSettings -> ResponseTimeout #

Default timeout to be applied to requests which do not provide a timeout value.

Default is 30 seconds

Since: 0.5.0

managerRetryableException :: ManagerSettings -> SomeException -> Bool #

Exceptions for which we should retry our request if we were reusing an already open connection. In the case of IOExceptions, for example, we assume that the connection was closed on the server and therefore open a new one.

Since 0.1.0

managerWrapException :: ManagerSettings -> forall a. Request -> IO a -> IO a #

Action wrapped around all attempted `Request`s, usually used to wrap up exceptions in library-specific types.

Default: wrap all `IOException`s in the `InternalException` constructor.

Since: 0.5.0

managerIdleConnectionCount :: ManagerSettings -> Int #

Total number of idle connection to keep open at a given time.

This limit helps deal with the case where you are making a large number of connections to different hosts. Without this limit, you could run out of file descriptors.

Default: 512

Since 0.3.7

managerModifyRequest :: ManagerSettings -> Request -> IO Request #

Perform the given modification to a `Request` before performing it.

Default: no modification

Since 0.4.4

managerModifyResponse :: ManagerSettings -> Response BodyReader -> IO (Response BodyReader) #

Perform the given modification to a `Response` after receiving it.

Default: no modification

Since: 0.5.5

### Manager proxy settings

managerSetProxy :: ProxyOverride -> ManagerSettings -> ManagerSettings #

Set the proxy override value, for both HTTP (insecure) and HTTPS (insecure) connections.

Since 0.4.7

managerSetInsecureProxy :: ProxyOverride -> ManagerSettings -> ManagerSettings #

Set the proxy override value, only for HTTP (insecure) connections.

Since 0.4.7

managerSetSecureProxy :: ProxyOverride -> ManagerSettings -> ManagerSettings #

Set the proxy override value, only for HTTPS (secure) connections.

Since 0.4.7

data ProxyOverride #

How the HTTP proxy server settings should be discovered.

Since 0.4.7

proxyFromRequest :: ProxyOverride #

Get the proxy settings from the `Request` itself.

Since 0.4.7

noProxy :: ProxyOverride #

Never connect using a proxy, regardless of the proxy value in the `Request`.

Since 0.4.7

useProxy :: Proxy -> ProxyOverride #

Use the given proxy settings, regardless of the proxy value in the `Request`.

Since 0.4.7

proxyEnvironment #

Arguments

:: Maybe Proxy | fallback if no environment set
-> ProxyOverride |

Get the proxy settings from the default environment variable (`http_proxy` for insecure, `https_proxy` for secure). If no variable is set, then fall back to the given value. `Nothing` is equivalent to `noProxy`, `Just` is equivalent to `useProxy`.

Since 0.4.7

proxyEnvironmentNamed #

Arguments

:: Text | environment variable name
-> Maybe Proxy | fallback if no environment set
-> ProxyOverride |

Same as `proxyEnvironment`, but instead of default environment variable names, allows you to set your own name.

Since 0.4.7

defaultProxy :: ProxyOverride #

The default proxy settings for a manager. In particular: if the `http_proxy` (or `https_proxy`) environment variable is set, use it. Otherwise, use the values in the `Request`.

Since 0.4.7

### Response timeouts

data ResponseTimeout #

How to deal with timing out a response

Since: 0.5.0

Instances

Eq ResponseTimeout # |
Methods (==) :: ResponseTimeout -> ResponseTimeout -> Bool # (/=) :: ResponseTimeout -> ResponseTimeout -> Bool #
Show ResponseTimeout # |
Methods showsPrec :: Int -> ResponseTimeout -> ShowS # show :: ResponseTimeout -> String # showList :: [ResponseTimeout] -> ShowS #

responseTimeoutMicro :: Int -> ResponseTimeout #

Specify a response timeout in microseconds

Since: 0.5.0

responseTimeoutNone :: ResponseTimeout #

Do not have a response timeout

Since: 0.5.0

responseTimeoutDefault :: ResponseTimeout #

Use the default response timeout

When used on a `Request`, means: use the manager's timeout value

When used on a `ManagerSettings`, means: default to 30 seconds

Since: 0.5.0

### Helpers

rawConnectionModifySocket :: (Socket -> IO ()) -> IO (Maybe HostAddress -> String -> Int -> IO Connection) #

A value for the `managerRawConnection` setting, but also allows you to modify the underlying `Socket` to set additional settings. For a motivating use case, see: https://github.com/snoyberg/http-client/issues/71.

Since 0.3.8

rawConnectionModifySocketSize :: (Socket -> IO ()) -> IO (Int -> Maybe HostAddress -> String -> Int -> IO Connection) #

Same as `rawConnectionModifySocket`, but also takes in a chunk size.

Since: 0.5.2

# Request

The way you parse string of characters to construct a `Request` will determine whether exceptions will be thrown on non-2XX response status codes. This is because the behavior is controlled by a setting in `Request` itself (see `checkResponse`) and different parsing functions set it to different `IO` actions.

parseUrl :: MonadThrow m => String -> m Request #

Deprecated: Please use parseUrlThrow, parseRequest, or parseRequest_ instead

Deprecated synonym for `parseUrlThrow`. You probably want `parseRequest` or `parseRequest_` instead.

Since: 0.1.0

parseUrlThrow :: MonadThrow m => String -> m Request #

Same as `parseRequest`, except will throw an `HttpException` in the event of a non-2XX response.

Since: 0.4.30

parseRequest :: MonadThrow m => String -> m Request #

Convert a URL into a `Request`.

This defaults some of the values in `Request`, such as setting `method` to GET and `requestHeaders` to `[]`.

Since this function uses `MonadThrow`, the return monad can be anything that is an instance of `MonadThrow`, such as `IO` or `Maybe`.

You can place the request method at the beginning of the URL separated by a space, e.g.:

@@`parseRequeset "POST http://httpbin.org/post"` @@

Note that the request method must be provided as all capital letters.

`Request` created by this function won't cause exceptions on non-2XX response status codes.

Since: 0.4.30

parseRequest_ :: String -> Request #

Same as `parseRequest`, but in the cases of a parse error generates an impure exception. Mostly useful for static strings which are known to be correctly formatted.

defaultRequest :: Request #

A default request value

Since: 0.4.30

applyBasicAuth :: ByteString -> ByteString -> Request -> Request #

Add a Basic Auth header (with the specified user name and password) to the given Request. Ignore error handling:

     applyBasicAuth "user" "pass" $ parseRequest_ url

NOTE: The function `applyDigestAuth` is provided by the `http-client-tls` package instead of this package due to extra dependencies. Please use that package if you need to use digest authentication.

Since 0.1.0

urlEncodedBody :: [(ByteString, ByteString)] -> Request -> Request #

Add url-encoded parameters to the `Request`.

This sets a new `requestBody`, adds a content-type request header and changes the `method` to POST.

Since 0.1.0

getUri :: Request -> URI #

Extract a `URI` from the request.

Since 0.1.0

setRequestIgnoreStatus :: Request -> Request #

Modify the request so that non-2XX status codes do not generate a runtime `StatusCodeException`.

Since: 0.4.29

setQueryString :: [(ByteString, Maybe ByteString)] -> Request -> Request #

Set the query string to the given key/value pairs.

Since 0.3.6

## Request type and fields

data Request #

All information on how to connect to a host and what should be sent in the HTTP request.

If you simply wish to download from a URL, see `parseRequest`.

The constructor for this data type is not exposed. Instead, you should use either the `defaultRequest` value, or `parseRequest` to construct from a URL, and then use the records below to make modifications. This approach allows http-client to add configuration options without breaking backwards compatibility.

For example, to construct a POST request, you could do something like:

    initReq <- parseRequest "http://www.example.com/path"
    let req = initReq
                { method = "POST"
                }

For more information, please see http://www.yesodweb.com/book/settings-types.

Since 0.1.0

Instances

Show Request # |
Methods showsPrec :: Int -> Request -> ShowS # show :: Request -> String # showList :: [Request] -> ShowS #

method :: Request -> Method #

HTTP request method, eg GET, POST.

Since 0.1.0

secure :: Request -> Bool #

Whether to use HTTPS (ie, SSL).

Since 0.1.0

host :: Request -> ByteString #

Requested host name, used for both the IP address to connect to and the `host` request header.

Since 0.1.0

port :: Request -> Int #

The port to connect to. Also used for generating the `host` request header.

Since 0.1.0

path :: Request -> ByteString #

Everything from the host to the query string.

Since 0.1.0

queryString :: Request -> ByteString #

Query string appended to the path.

Since 0.1.0

requestHeaders :: Request -> RequestHeaders #

Custom HTTP request headers

The Content-Length and Transfer-Encoding headers are set automatically by this module, and shall not be added to `requestHeaders`.

If not provided by the user, `Host` will automatically be set based on the `host` and `port` fields.

Moreover, the Accept-Encoding header is set implicitly to gzip for convenience by default. This behaviour can be overridden if needed, by setting the header explicitly to a different value. In order to omit the Accept-Header altogether, set it to the empty string "". If you need an empty Accept-Header (i.e. requesting the identity encoding), set it to a non-empty white-space string, e.g. " ". See RFC 2616 section 14.3 for details about the semantics of the Accept-Header field. If you request a content-encoding not supported by this module, you will have to decode it yourself (see also the `decompress` field).

Note: Multiple header fields with the same field-name will result in multiple header fields being sent and therefore it's the responsibility of the client code to ensure that the rules from RFC 2616 section 4.2 are honoured.

Since 0.1.0

requestBody :: Request -> RequestBody #

Request body to be sent to the server.

Since 0.1.0

proxy :: Request -> Maybe Proxy #

Optional HTTP proxy.

Since 0.1.0

applyBasicProxyAuth :: ByteString -> ByteString -> Request -> Request #

Add a Proxy-Authorization header (with the specified username and password) to the given `Request`. Ignore error handling:

    applyBasicProxyAuth "user" "pass" <$> parseRequest "http://example.org"

Since 0.3.4

decompress :: Request -> ByteString -> Bool #

Predicate to specify whether gzipped data should be decompressed on the fly (see `alwaysDecompress` and `browserDecompress`). Argument is the mime type. Default: browserDecompress.

Since 0.1.0

redirectCount :: Request -> Int #

How many redirects to follow when getting a resource. 0 means follow no redirects. Default value: 10.

Since 0.1.0

checkResponse :: Request -> Request -> Response BodyReader -> IO () #

Check the response immediately after receiving the status and headers. This can be useful for throwing exceptions on non-success status codes.

In previous versions of http-client, this went under the name `checkStatus`, but was renamed to avoid confusion about the new default behavior (doing nothing).

Since: 0.5.0

responseTimeout :: Request -> ResponseTimeout #

Number of microseconds to wait for a response. If `Nothing`, will wait indefinitely. Default: use `managerResponseTimeout` (which by default is 30 seconds).

Since 0.1.0

cookieJar :: Request -> Maybe CookieJar #

A user-defined cookie jar. If `Nothing`, no cookie handling will take place, "Cookie" headers in `requestHeaders` will be sent raw, and `responseCookieJar` will be empty.

Since 0.1.0

requestVersion :: Request -> HttpVersion #

HTTP version to send to server.

Default: HTTP 1.1

Since 0.4.3

## Request body

data RequestBody #

When using one of the `RequestBodyStream` / `RequestBodyStreamChunked` constructors, you must ensure that the `GivesPopper` can be called multiple times. Usually this is not a problem.

The `RequestBodyStreamChunked` will send a chunked request body. Note that not all servers support this. Only use `RequestBodyStreamChunked` if you know the server you're sending to supports chunked request bodies.

Since 0.1.0

Constructors

RequestBodyLBS ByteString |
RequestBodyBS ByteString |
RequestBodyBuilder Int64 Builder |
RequestBodyStream Int64 (GivesPopper ()) |
RequestBodyStreamChunked (GivesPopper ()) |
RequestBodyIO (IO RequestBody) | Allows creation of a `RequestBody` inside the `IO` monad, which is useful for making easier APIs (like `setRequestBodyFile`). Since: 0.4.28

Instances

IsString RequestBody # | Since 0.4.12
Methods fromString :: String -> RequestBody #
Monoid RequestBody # |
Methods mempty :: RequestBody # mappend :: RequestBody -> RequestBody -> RequestBody # mconcat :: [RequestBody] -> RequestBody #

type Popper = IO ByteString #

A function which generates successive chunks of a request body, provider a single empty bytestring when no more data is available.

Since 0.1.0

type NeedsPopper a = Popper -> IO a #

A function which must be provided with a `Popper`.

Since 0.1.0

type GivesPopper a = NeedsPopper a -> IO a #

A function which will provide a `Popper` to a `NeedsPopper`. This seemingly convoluted structure allows for creation of request bodies which allocate scarce resources in an exception safe manner.

Since 0.1.0

streamFile :: FilePath -> IO RequestBody #

Send a file as the request body.

It is expected that the file size does not change between calling `streamFile` and making any requests using this request body.

Since 0.4.9

observedStreamFile :: (StreamFileStatus -> IO ()) -> FilePath -> IO RequestBody #

Send a file as the request body, while observing streaming progress via a `PopObserver`. Observations are made between reading and sending a chunk.

It is expected that the file size does not change between calling `observedStreamFile` and making any requests using this request body.

Since 0.4.9

data StreamFileStatus #

Status of streaming a request body from a file.

Since 0.4.9

Constructors

StreamFileStatus |
Fields fileSize :: Int64 readSoFar :: Int64 thisChunkSize :: Int

Instances

Eq StreamFileStatus # |
Methods (==) :: StreamFileStatus -> StreamFileStatus -> Bool # (/=) :: StreamFileStatus -> StreamFileStatus -> Bool #
Ord StreamFileStatus # |
Methods compare :: StreamFileStatus -> StreamFileStatus -> Ordering # (<) :: StreamFileStatus -> StreamFileStatus -> Bool # (<=) :: StreamFileStatus -> StreamFileStatus -> Bool # (>) :: StreamFileStatus -> StreamFileStatus -> Bool # (>=) :: StreamFileStatus -> StreamFileStatus -> Bool # max :: StreamFileStatus -> StreamFileStatus -> StreamFileStatus # min :: StreamFileStatus -> StreamFileStatus -> StreamFileStatus #
Show StreamFileStatus # |
Methods showsPrec :: Int -> StreamFileStatus -> ShowS # show :: StreamFileStatus -> String # showList :: [StreamFileStatus] -> ShowS #

# Response

data Response body #

A simple representation of the HTTP response.

Since 0.1.0

Instances

Functor Response # |
Methods fmap :: (a -> b) -> Response a -> Response b # (<$) :: a -> Response b -> Response a #
Foldable Response # |
Methods fold :: Monoid m => Response m -> m # foldMap :: Monoid m => (a -> m) -> Response a -> m # foldr :: (a -> b -> b) -> b -> Response a -> b # foldr' :: (a -> b -> b) -> b -> Response a -> b # foldl :: (b -> a -> b) -> b -> Response a -> b # foldl' :: (b -> a -> b) -> b -> Response a -> b # foldr1 :: (a -> a -> a) -> Response a -> a # foldl1 :: (a -> a -> a) -> Response a -> a # toList :: Response a -> [a] # null :: Response a -> Bool # length :: Response a -> Int # elem :: Eq a => a -> Response a -> Bool # maximum :: Ord a => Response a -> a # minimum :: Ord a => Response a -> a # sum :: Num a => Response a -> a # product :: Num a => Response a -> a #
Traversable Response # |
Methods traverse :: Applicative f => (a -> f b) -> Response a -> f (Response b) # sequenceA :: Applicative f => Response (f a) -> f (Response a) # mapM :: Monad m => (a -> m b) -> Response a -> m (Response b) # sequence :: Monad m => Response (m a) -> m (Response a) #
Eq body => Eq (Response body) # |
Methods (==) :: Response body -> Response body -> Bool # (/=) :: Response body -> Response body -> Bool #
Show body => Show (Response body) # |
Methods showsPrec :: Int -> Response body -> ShowS # show :: Response body -> String # showList :: [Response body] -> ShowS #

responseStatus :: Response body -> Status #

Status code of the response.

Since 0.1.0

responseVersion :: Response body -> HttpVersion #

HTTP version used by the server.

Since 0.1.0

responseHeaders :: Response body -> ResponseHeaders #

Response headers sent by the server.

Since 0.1.0

responseBody :: Response body -> body #

Response body sent by the server.

Since 0.1.0

responseCookieJar :: Response body -> CookieJar #

Cookies set on the client after interacting with the server. If cookies have been disabled by setting `cookieJar` to `Nothing`, then this will always be empty.

Since 0.1.0

## Response body

type BodyReader = IO ByteString #

An `IO` action that represents an incoming response body coming from the server. Data provided by this action has already been gunzipped and de-chunked, and respects any content-length headers present.

The action gets a single chunk of data from the response body, or an empty bytestring if no more data is available.

Since 0.4.0

brRead :: BodyReader -> IO ByteString #

Get a single chunk of data from the response body, or an empty bytestring if no more data is available.

Note that in order to consume the entire request body, you will need to repeatedly call this function until you receive an empty `ByteString` as a result.

Since 0.1.0

brReadSome :: BodyReader -> Int -> IO ByteString #

Continuously call `brRead`, building up a lazy ByteString until a chunk is constructed that is at least as many bytes as requested.

Since 0.4.20

brConsume :: BodyReader -> IO [ByteString] #

Strictly consume all remaining chunks of data from the stream.

Since 0.1.0

# Advanced connection creation

makeConnection #

Arguments

:: IO ByteString | read
-> (ByteString -> IO ()) | write
-> IO () | close
-> IO Connection |

Create a new `Connection` from a read, write, and close function.

Since: 0.5.3

socketConnection #

Arguments

:: Socket |
-> Int | chunk size
-> IO Connection |

Create a new `Connection` from a `Socket`.

Since: 0.5.3

# Misc

data HttpException #

An exception which may be generated by this library

Since: 0.5.0

Constructors

HttpExceptionRequest Request HttpExceptionContent | Most exceptions are specific to a `Request`. Inspect the `HttpExceptionContent` value for details on what occurred. Since: 0.5.0
InvalidUrlException String String | A URL (first field) is invalid for a given reason (second argument). Since: 0.5.0

Instances

Show HttpException # |
Methods showsPrec :: Int -> HttpException -> ShowS # show :: HttpException -> String # showList :: [HttpException] -> ShowS #
Exception HttpException # |
Methods toException :: HttpException -> SomeException # fromException :: SomeException -> Maybe HttpException # displayException :: HttpException -> String #

data HttpExceptionContent #

Constructors

StatusCodeException (Response ()) ByteString | Generated by the `parseUrlThrow` function when the server returns a non-2XX response status code. May include the beginning of the response body. Since: 0.5.0
TooManyRedirects [Response ByteString] | The server responded with too many redirects for a request. Contains the list of encountered responses containing redirects in reverse chronological order; including last redirect, which triggered the exception and was not followed. Since: 0.5.0
OverlongHeaders | Either too many headers, or too many total bytes in a single header, were returned by the server, and the memory exhaustion protection in this library has kicked in. Since: 0.5.0
ResponseTimeout | The server took too long to return a response. This can be altered via `responseTimeout` or `managerResponseTimeout`. Since: 0.5.0
ConnectionTimeout | Attempting to connect to the server timed out. Since: 0.5.0
ConnectionFailure SomeException | An exception occured when trying to connect to the server. Since: 0.5.0
InvalidStatusLine ByteString | The status line returned by the server could not be parsed. Since: 0.5.0
InvalidHeader ByteString | The given response header line could not be parsed Since: 0.5.0
InternalException SomeException | An exception was raised by an underlying library when performing the request. Most often, this is caused by a failing socket action or a TLS exception. Since: 0.5.0
ProxyConnectException ByteString Int Status | A non-200 status code was returned when trying to connect to the proxy server on the given host and port. Since: 0.5.0
NoResponseDataReceived | No response data was received from the server at all. This exception may deserve special handling within the library, since it may indicate that a pipelining has been used, and a connection thought to be open was in fact closed. Since: 0.5.0
TlsNotSupported | Exception thrown when using a `Manager` which does not have support for secure connections. Typically, you will want to use `tlsManagerSettings` from `http-client-tls` to overcome this. Since: 0.5.0
WrongRequestBodyStreamSize Word64 Word64 | The request body provided did not match the expected size. Provides the expected and actual size. Since: 0.4.31
ResponseBodyTooShort Word64 Word64 | The returned response body is too short. Provides the expected size and actual size. Since: 0.5.0
InvalidChunkHeaders | A chunked response body had invalid headers. Since: 0.5.0
IncompleteHeaders | An incomplete set of response headers were returned. Since: 0.5.0
InvalidDestinationHost ByteString | The host we tried to connect to is invalid (e.g., an empty string).
HttpZlibException ZlibException | An exception was thrown when inflating a response body. Since: 0.5.0
InvalidProxyEnvironmentVariable Text Text | Values in the proxy environment variable were invalid. Provides the environment variable name and its value. Since: 0.5.0
ConnectionClosed | Attempted to use a `Connection` which was already closed Since: 0.5.0

Instances

Show HttpExceptionContent # |
Methods showsPrec :: Int -> HttpExceptionContent -> ShowS # show :: HttpExceptionContent -> String # showList :: [HttpExceptionContent] -> ShowS #

data Cookie #

Constructors

Cookie |
Fields cookie_name :: ByteString cookie_value :: ByteString cookie_expiry_time :: UTCTime cookie_domain :: ByteString cookie_path :: ByteString cookie_creation_time :: UTCTime cookie_last_access_time :: UTCTime cookie_persistent :: Bool cookie_host_only :: Bool cookie_secure_only :: Bool cookie_http_only :: Bool

Instances

Eq Cookie # |
Methods (==) :: Cookie -> Cookie -> Bool # (/=) :: Cookie -> Cookie -> Bool #
Ord Cookie # |
Methods compare :: Cookie -> Cookie -> Ordering # (<) :: Cookie -> Cookie -> Bool # (<=) :: Cookie -> Cookie -> Bool # (>) :: Cookie -> Cookie -> Bool # (>=) :: Cookie -> Cookie -> Bool # max :: Cookie -> Cookie -> Cookie # min :: Cookie -> Cookie -> Cookie #
Read Cookie # |
Methods readsPrec :: Int -> ReadS Cookie # readList :: ReadS [Cookie] # readPrec :: ReadPrec Cookie # readListPrec :: ReadPrec [Cookie] #
Show Cookie # |
Methods showsPrec :: Int -> Cookie -> ShowS # show :: Cookie -> String # showList :: [Cookie] -> ShowS #

data CookieJar #

Instances

Eq CookieJar # |
Methods (==) :: CookieJar -> CookieJar -> Bool # (/=) :: CookieJar -> CookieJar -> Bool #
Read CookieJar # |
Methods readsPrec :: Int -> ReadS CookieJar # readList :: ReadS [CookieJar] # readPrec :: ReadPrec CookieJar # readListPrec :: ReadPrec [CookieJar] #
Show CookieJar # |
Methods showsPrec :: Int -> CookieJar -> ShowS # show :: CookieJar -> String # showList :: [CookieJar] -> ShowS #
Monoid CookieJar # | Since 1.9
Methods mempty :: CookieJar # mappend :: CookieJar -> CookieJar -> CookieJar # mconcat :: [CookieJar] -> CookieJar #

data Proxy #

Define a HTTP proxy, consisting of a hostname and port number.

Constructors

Proxy |
Fields proxyHost :: ByteString The host name of the HTTP proxy. proxyPort :: Int The port number of the HTTP proxy.

Instances

Eq Proxy # |
Methods (==) :: Proxy -> Proxy -> Bool # (/=) :: Proxy -> Proxy -> Bool #
Ord Proxy # |
Methods compare :: Proxy -> Proxy -> Ordering # (<) :: Proxy -> Proxy -> Bool # (<=) :: Proxy -> Proxy -> Bool # (>) :: Proxy -> Proxy -> Bool # (>=) :: Proxy -> Proxy -> Bool # max :: Proxy -> Proxy -> Proxy # min :: Proxy -> Proxy -> Proxy #
Read Proxy # |
Methods readsPrec :: Int -> ReadS Proxy # readList :: ReadS [Proxy] # readPrec :: ReadPrec Proxy # readListPrec :: ReadPrec [Proxy] #
Show Proxy # |
Methods showsPrec :: Int -> Proxy -> ShowS # show :: Proxy -> String # showList :: [Proxy] -> ShowS #

# Cookies

updateCookieJar #

Arguments

:: Response a | Response received from server
-> Request | Request which generated the response
-> UTCTime | Value that should be used as "now"
-> CookieJar | Current cookie jar
-> (CookieJar, Response a) | (Updated cookie jar with cookies from the Response, The response stripped of any "Set-Cookie" header)

This applies `receiveSetCookie` to a given Response

receiveSetCookie #

Arguments

:: SetCookie | The `SetCookie` the cookie jar is receiving
-> Request | The request that originated the response that yielded the `SetCookie`
-> UTCTime | Value that should be used as "now"
-> Bool | Whether or not this request is coming from an "http" source (not javascript or anything like that)
-> CookieJar | Input cookie jar to modify
-> CookieJar | Updated cookie jar

This corresponds to the algorithm described in Section 5.3 "Storage Model" This function consists of calling `generateCookie` followed by `insertCheckedCookie`. Use this function if you plan to do both in a row. `generateCookie` and `insertCheckedCookie` are only provided for more fine-grained control.

generateCookie #

Arguments

:: SetCookie | The `SetCookie` we are encountering
-> Request | The request that originated the response that yielded the `SetCookie`
-> UTCTime | Value that should be used as "now"
-> Bool | Whether or not this request is coming from an "http" source (not javascript or anything like that)
-> Maybe Cookie | The optional output cookie

Turn a SetCookie into a Cookie, if it is valid

insertCheckedCookie #

Arguments

:: Cookie | The `SetCookie` the cookie jar is receiving
-> CookieJar | Input cookie jar to modify
-> Bool | Whether or not this request is coming from an "http" source (not javascript or anything like that)
-> CookieJar | Updated (or not) cookie jar

Insert a cookie created by generateCookie into the cookie jar (or not if it shouldn't be allowed in)

insertCookiesIntoRequest #

Arguments

:: Request | The request to insert into
-> CookieJar | Current cookie jar
-> UTCTime | Value that should be used as "now"
-> (Request, CookieJar) | (Ouptut request, Updated cookie jar (last-access-time is updated))

This applies the `computeCookieString` to a given Request

computeCookieString #

Arguments

:: Request | Input request
-> CookieJar | Current cookie jar
-> UTCTime | Value that should be used as "now"
-> Bool | Whether or not this request is coming from an "http" source (not javascript or anything like that)
-> (ByteString, CookieJar) | (Contents of a "Cookie" header, Updated cookie jar (last-access-time is updated))

This corresponds to the algorithm described in Section 5.4 "The Cookie Header"

evictExpiredCookies #

Arguments

:: CookieJar | Input cookie jar
-> UTCTime | Value that should be used as "now"
-> CookieJar | Filtered cookie jar

This corresponds to the eviction algorithm described in Section 5.3 "Storage Model"

createCookieJar :: [Cookie] -> CookieJar #

destroyCookieJar :: CookieJar -> [Cookie] #

pathMatches :: ByteString -> ByteString -> Bool #

This corresponds to the subcomponent algorithm entitled "Path-Match" detailed in section 5.1.4

removeExistingCookieFromCookieJar :: Cookie -> CookieJar -> (Maybe Cookie, CookieJar) #

domainMatches #

Arguments

:: ByteString | Domain to test
-> ByteString | Domain from a cookie
-> Bool |

This corresponds to the subcomponent algorithm entitled "Domain Matching" detailed in section 5.1.3

isIpAddress :: ByteString -> Bool #

defaultPath :: Request -> ByteString #

This corresponds to the subcomponent algorithm entitled "Paths" detailed in section 5.1.4