http-conduit-2.1.8: HTTP client package with conduit interface and HTTPS support.

Safe HaskellNone




This module contains everything you need to initiate HTTP connections. If you want a simple interface based on URLs, you can use simpleHttp. If you want raw power, http is the underlying workhorse of this package. Some examples:

-- Just download an HTML document and print it.
import Network.HTTP.Conduit
import qualified Data.ByteString.Lazy as L

main = simpleHttp "" >>= L.putStr

This example uses interleaved IO to write the response body to a file in constant memory space.

import Data.Conduit.Binary (sinkFile) -- Exported from the package conduit-extra
import Network.HTTP.Conduit
import qualified Data.Conduit as C
import Control.Monad.Trans.Resource (runResourceT)

main :: IO ()
main = do
     request <- parseUrl ""
     manager <- newManager tlsManagerSettings
     runResourceT $ do
         response <- http request manager
         responseBody response C.$$+- sinkFile "google.html"

The following headers are automatically set by this module, and should not be added to requestHeaders:

  • Cookie
  • Content-Length
  • Transfer-Encoding

Note: In previous versions, the Host header would be set by this module in all cases. Starting from 1.6.1, if a Host header is present in requestHeaders, it will be used in place of the header this module would have generated. This can be useful for calling a server which utilizes virtual hosting.

Use cookieJar If you want to supply cookies with your request:

{-# LANGUAGE OverloadedStrings #-}
import Network.HTTP.Conduit
import Network
import Data.Time.Clock
import Data.Time.Calendar
import qualified Control.Exception as E
import Network.HTTP.Types.Status (statusCode)

past :: UTCTime
past = UTCTime (ModifiedJulianDay 56200) (secondsToDiffTime 0)

future :: UTCTime
future = UTCTime (ModifiedJulianDay 562000) (secondsToDiffTime 0)

cookie :: Cookie
cookie = Cookie { cookie_name = "password_hash"
                , cookie_value = "abf472c35f8297fbcabf2911230001234fd2"
                , cookie_expiry_time = future
                , cookie_domain = ""
                , cookie_path = "/"
                , cookie_creation_time = past
                , cookie_last_access_time = past
                , cookie_persistent = False
                , cookie_host_only = False
                , cookie_secure_only = False
                , cookie_http_only = False

main = withSocketsDo $ do
     request' <- parseUrl ""
     manager <- newManager tlsManagerSettings
     let request = request' { cookieJar = Just $ createCookieJar [cookie] }
     (fmap Just (httpLbs request manager)) `E.catch`
             (\(StatusCodeException s _ _) ->
               if statusCode s==403 then (putStrLn "login failed" >> return Nothing) else return Nothing)

Any network code on Windows requires some initialization, and the network library provides withSocketsDo to perform it. Therefore, proper usage of this library will always involve calling that function at some point. The best approach is to simply call them at the beginning of your main function, such as:

import Network.HTTP.Conduit
import qualified Data.ByteString.Lazy as L
import Network (withSocketsDo)

main = withSocketsDo
     $ simpleHttp "" >>= L.putStr

Cookies are implemented according to RFC 6265.

Note that by default, the functions in this package will throw exceptions for non-2xx status codes. If you would like to avoid this, you should use checkStatus, e.g.:

import Data.Conduit.Binary (sinkFile)
import Network.HTTP.Conduit
import qualified Data.Conduit as C
import Network

main :: IO ()
main = withSocketsDo $ do
     request' <- parseUrl ""
     let request = request' { checkStatus = \_ _ _ -> Nothing }
     manager <- newManager tlsManagerSettings
     res <- httpLbs request manager
     print res

By default, when connecting to websites using HTTPS, functions in this package will throw an exception if the TLS certificate doesn't validate. To continue the HTTPS transaction even if the TLS cerficate validation fails, you should use mkManagerSetttings as follows:

import Network.Connection (TLSSettings (..))
import Network.HTTP.Conduit

main :: IO ()
main = do
    request <- parseUrl ""
    let settings = mkManagerSettings (TLSSettingsSimple True False False) Nothing
    manager <- newManager settings
    res <- httpLbs request manager
    print res

For more information, please be sure to read the documentation in the Network.HTTP.Client module.


Perform a request

simpleHttp :: MonadIO m => String -> m ByteString Source

Download the specified URL, following any redirects, and return the response body.

This function will throwIO an HttpException for any response with a non-2xx status code (besides 3xx redirects up to a limit of 10 redirects). It uses parseUrl to parse the input. This function essentially wraps httpLbs.

Note: Even though this function returns a lazy bytestring, it does not utilize lazy I/O, and therefore the entire response body will live in memory. If you want constant memory usage, you'll need to use the conduit package and http directly.

Note: This function creates a new Manager. It should be avoided in production code.

httpLbs :: MonadIO m => Request -> Manager -> m (Response ByteString) Source

Download the specified Request, returning the results as a Response.

This is a simplified version of http for the common case where you simply want the response data as a simple datatype. If you want more power, such as interleaved actions on the response body during download, you'll need to use http directly. This function is defined as:

httpLbs = lbsResponse <=< http

Even though the Response contains a lazy bytestring, this function does not utilize lazy I/O, and therefore the entire response body will live in memory. If you want constant memory usage, you'll need to use conduit packages's Source returned by http.

This function will throwIO an HttpException for any response with a non-2xx status code (besides 3xx redirects up to a limit of 10 redirects). This behavior can be modified by changing the checkStatus field of your request.

Note: Unlike previous versions, this function will perform redirects, as specified by the redirectCount setting.


data Proxy :: *

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




proxyHost :: ByteString

The host name of the HTTP proxy.

proxyPort :: Int

The port number of the HTTP proxy.

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


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 parseUrl.

The constructor for this data type is not exposed. Instead, you should use either the def method to retrieve a default instance, or parseUrl 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 <- parseUrl ""
let req = initReq
            { method = "POST"

For more information, please see

Since 0.1.0

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

hostAddress :: Request -> Maybe HostAddress

Optional resolved host address. May not be used by all backends.

Since 0.1.0

rawBody :: Request -> Bool

If True, a chunked and/or gzipped body will not be decoded. Use with caution.

Since 0.1.0

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

checkStatus :: Request -> Status -> ResponseHeaders -> CookieJar -> Maybe SomeException

Check the status code. Note that this will run after all redirects are performed. Default: return a StatusCodeException on non-2XX responses.

Since 0.1.0

responseTimeout :: Request -> Maybe Int

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

getConnectionWrapper :: Request -> Maybe Int -> HttpException -> IO (ConnRelease, Connection, ManagedConn) -> IO (Maybe Int, (ConnRelease, Connection, ManagedConn))

Wraps the calls for getting new connections. This can be useful for instituting some kind of timeouts. The first argument is the value of responseTimeout. Second argument is the exception to be thrown on failure.

Default: If responseTimeout is Nothing, does nothing. Otherwise, institutes timeout, and returns remaining time for responseTimeout.

Since 0.1.0

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

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

Since 0.3.6

Request body


data Response body :: * -> *

A simple representation of the HTTP response.

Since 0.1.0


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


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

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 ()

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 :: (MonadIO m, MonadBaseControl IO m) => (Manager -> ResourceT m a) -> m a Source

Deprecated: Please use newManager tlsManagerSettings

withManagerSettings :: (MonadIO m, MonadBaseControl IO m) => ManagerSettings -> (Manager -> ResourceT m a) -> m a Source

Deprecated: Please use newManager


data ManagerSettings :: *

Settings for a Manager. Please use the defaultManagerSettings function and then modify individual settings. For more information, see

Since 0.1.0

conduitManagerSettings :: ManagerSettings Source

Deprecated: Use tlsManagerSettings

managerConnCount :: ManagerSettings -> Int

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

Since 0.1.0

managerResponseTimeout :: ManagerSettings -> Maybe Int

Default timeout (in microseconds) to be applied to requests which do not provide a timeout value.

Default is 30 seconds

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


Utility functions

parseUrl :: 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.

Since 0.1.0

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" $ fromJust $ parseUrl url

Since 0.1.0

addProxy :: ByteString -> Int -> Request -> Request

Add a proxy to the Request so that the Request when executed will use the provided proxy.

Since 0.1.0

getRedirectedRequest :: Request -> ResponseHeaders -> CookieJar -> Int -> Maybe Request

If a request is a redirection (status code 3xx) this function will create a new request from the old request, the server headers returned with the redirection, and the redirection code itself. This function returns Nothing if the code is not a 3xx, there is no location header included, or if the redirected response couldn't be parsed with parseUrl.

If a user of this library wants to know the url chain that results from a specific request, that user has to re-implement the redirect-following logic themselves. An example of that might look like this:

myHttp req man = do
   (res, redirectRequests) <- (`runStateT` []) $
            (\req' -> do
               res <- http req'{redirectCount=0} man
               modify (\rqs -> req' : rqs)
               return (res, getRedirectedRequest req' (responseHeaders res) (responseCookieJar res) (W.statusCode (responseStatus res))
   applyCheckStatus (checkStatus req) res
   return redirectRequests

Decompression predicates

alwaysDecompress :: ByteString -> Bool

Always decompress a compressed stream.

browserDecompress :: ByteString -> Bool

Decompress a compressed stream unless the content-type is 'application/x-tar'.

Request bodies

Network.HTTP.Client.MultipartFormData provides an API for building form-data request bodies.

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


data HttpException :: *


StatusCodeException Status ResponseHeaders CookieJar 
InvalidUrlException String String 
TooManyRedirects [Response ByteString]

List of encountered responses containing redirects in reverse chronological order; including last redirect, which triggered the exception and was not followed.

UnparseableRedirect (Response ByteString)

Response containing unparseable redirect.

HttpParserException String 
FailedConnectionException String Int


Note that in old versions of http-client and http-conduit, this exception would indicate a failed attempt to create a connection. However, since (at least) http-client 0.4, it indicates a timeout occurred while trying to establish the connection. For more information on this, see:

FailedConnectionException2 String Int Bool SomeException


InvalidStatusLine ByteString 
InvalidHeader ByteString 
InternalIOException IOException 
ProxyConnectException ByteString Int (Either ByteString HttpException)


TlsException SomeException 
ResponseBodyTooShort Word64 Word64

Expected size/actual size.

Since 1.9.4


Since 1.9.4

InvalidDestinationHost ByteString 
HttpZlibException ZlibException

Since 0.3

InvalidProxyEnvironmentVariable Text Text

Environment name and value

Since 0.4.7


Detect a case where both the content-length header and transfer-encoding: chunked are used. Since 0.4.8.

Since 0.4.11 this exception isn't thrown anymore.