Network.HTTP.Enumerator
Description
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.Enumerator import qualified Data.ByteString.Lazy as L main = simpleHttp "http://www.haskell.org/" >>= L.putStr
This example uses interleaved IO to write the response body to a file in
constant memory space. By using httpRedirect
, it will automatically
follow 3xx redirects.
import Data.Enumerator import Data.Enumerator.Binary import Network.HTTP.Enumerator import System.IO main :: IO () main = withFile "google.html" WriteMode $ \handle -> do request <- parseUrl "http://google.com/" withManager $ \manager -> do run_ $ httpRedirect request (\_ _ -> iterHandle handle) manager
The following headers are automatically set by this module, and should not
be added to requestHeaders
:
- Content-Length
- Host
- Accept-Encoding (not currently set, but client usage of this variable will cause breakage).
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.Enumerator import qualified Data.ByteString.Lazy as L import Network (withSocketsDo) main = withSocketsDo $ simpleHttp "http://www.haskell.org/" >>= L.putStr
- simpleHttp :: (MonadIO m, Failure HttpException m) => String -> m ByteString
- httpLbs :: MonadIO m => Request m -> Manager -> m Response
- httpLbsRedirect :: (MonadIO m, Failure HttpException m) => Request m -> Manager -> m Response
- http :: MonadIO m => Request m -> (Status -> ResponseHeaders -> Iteratee ByteString m a) -> Manager -> Iteratee ByteString m a
- httpRedirect :: (MonadIO m, Failure HttpException m) => Request m -> (Status -> ResponseHeaders -> Iteratee ByteString m a) -> Manager -> Iteratee ByteString m a
- redirectIter :: (MonadIO m, Failure HttpException m) => Int -> Request m -> (Status -> ResponseHeaders -> Iteratee ByteString m a) -> Manager -> Status -> ResponseHeaders -> Iteratee ByteString m a
- data Request m = Request {
- method :: Method
- secure :: Bool
- checkCerts :: [X509] -> IO Bool
- host :: Ascii
- port :: Int
- path :: Ascii
- queryString :: Query
- requestHeaders :: RequestHeaders
- requestBody :: RequestBody m
- data RequestBody m
- data Response = Response {}
- data Manager
- newManager :: IO Manager
- closeManager :: Manager -> IO ()
- withManager :: MonadControlIO m => (Manager -> m a) -> m a
- parseUrl :: Failure HttpException m => String -> m (Request m')
- semiParseUrl :: Failure HttpException m => String -> m (Request m')
- lbsIter :: Monad m => Status -> ResponseHeaders -> Iteratee ByteString m Response
- urlEncodedBody :: Monad m => [(ByteString, ByteString)] -> Request m' -> Request m
- data HttpException
Perform a request
simpleHttp :: (MonadIO m, Failure HttpException m) => String -> m ByteStringSource
Download the specified URL, following any redirects, and return the response body.
This function will failure
an HttpException
for any response with a
non-2xx status code. It uses parseUrl
to parse the input. This function
essentially wraps httpLbsRedirect
.
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 write your own
iteratee and use http
or httpRedirect
directly.
httpLbs :: MonadIO m => Request m -> Manager -> m ResponseSource
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 = http lbsIter
Please see lbsIter
for more information on how the Response
value is
created.
Even though a 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 write your own
iteratee and use http
or httpRedirect
directly.
httpLbsRedirect :: (MonadIO m, Failure HttpException m) => Request m -> Manager -> m ResponseSource
Download the specified Request
, returning the results as a Response
and automatically handling redirects.
This is a simplified version of httpRedirect
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 httpRedirect
directly. This function is defined as:
httpLbsRedirect = httpRedirect lbsIter
Please see lbsIter
for more information on how the Response
value is
created.
Even though a 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 write your own
iteratee and use http
or httpRedirect
directly.
http :: MonadIO m => Request m -> (Status -> ResponseHeaders -> Iteratee ByteString m a) -> Manager -> Iteratee ByteString m aSource
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 how the response should be handled. It's a
function that takes two arguments: the first is the HTTP status code of the
response, and the second is a list of all response headers. This module
exports lbsIter
, which generates a Response
value.
Note that this allows you to have fully interleaved IO actions during your HTTP download, making it possible to download very large responses in constant memory.
httpRedirect :: (MonadIO m, Failure HttpException m) => Request m -> (Status -> ResponseHeaders -> Iteratee ByteString m a) -> Manager -> Iteratee ByteString m aSource
Same as http
, but follows all 3xx redirect status codes that contain a
location header.
Arguments
:: (MonadIO m, Failure HttpException m) | |
=> Int | number of redirects to attempt |
-> Request m | Original request |
-> (Status -> ResponseHeaders -> Iteratee ByteString m a) | |
-> Manager | |
-> Status -> ResponseHeaders -> Iteratee ByteString m a |
Make a request automatically follow 3xx redirects.
Used internally by httpRedirect
and family.
Datatypes
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
.
Constructors
Request | |
Fields
|
data RequestBody m Source
When using the RequestBodyEnum
constructor and any function which calls
redirectIter
, you must ensure that the Enumerator
can be called multiple
times.
Manager
newManager :: IO ManagerSource
Create a new Manager
with no open connection.
closeManager :: Manager -> IO ()Source
withManager :: MonadControlIO m => (Manager -> m a) -> m aSource
Create a new Manager
, call the supplied function and then close it.
Utility functions
semiParseUrl :: Failure HttpException m => String -> m (Request m')Source
Same as parseUrl
, with one distinction: this function will not attempt
to parse the query string, but instead leave it with the path info. This can
be useful if you need precise control of the rendering of the query string,
such as using semicolons instead of ampersands.
lbsIter :: Monad m => Status -> ResponseHeaders -> Iteratee ByteString m ResponseSource
Convert the HTTP response into a Response
value.
Even though a 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 write your own
iteratee and use http
or httpRedirect
directly.
Request bodies
urlEncodedBody :: Monad m => [(ByteString, ByteString)] -> Request m' -> Request mSource
Add url-encoded paramters to the Request
.
This sets a new requestBody
, adds a content-type request header and
changes the method
to POST.