HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulehttp-conduit-2.3.9.1Haskell2010

Network.HTTP.Simple

Simplified interface for common HTTP client interactions. Tutorial available at https://haskell-lang.org/library/http-client

Important note: Request is an instance of IsString, and therefore recommended usage is to turn on OverloadedStrings, e.g.

{-# LANGUAGE OverloadedStrings #-}
import Network.HTTP.Simple
import qualified Data.ByteString.Char8 as B8

main :: IO ()
main = httpBS "http://example.com" >>= B8.putStrLn . getResponseBody

The IsString instance uses parseRequest behind the scenes and inherits its behavior.

  • 10 types
  • 44 values

Perform requests

8 declarations
valuehttpLBS :: MonadIO m => Request -> m (Response ByteString)
#

Perform an HTTP request and return the body as a lazy ByteString. Note that the entire value will be read into memory at once (no lazy I/O will be performed). The advantage of a lazy ByteString here (versus using httpBS) is--if needed--a better in-memory representation.

valuehttpJSON :: (MonadIO m, FromJSON a) => Request -> m (Response a)
#

Perform an HTTP request and parse the body as JSON. In the event of an JSON parse errors, a JSONException runtime exception will be thrown.

NOTE: Depends on the aeson cabal flag being enabled

valuehttpSource
  1. :: (MonadResource m, MonadIO n)
  2. => Request
  3. -> Response (ConduitM i ByteString n ()) -> ConduitM i o m r
  4. -> ConduitM i o m r
#

Perform an HTTP request, and get the response body as a Source.

The second argument to this function tells us how to make the Source from the Response itself. This allows you to perform actions with the status or headers, for example, in addition to the raw bytes themselves. If you just care about the response body, you can use getResponseBody as the second argument here.

{-# LANGUAGE OverloadedStrings #-}
import           Control.Monad.IO.Class       (liftIO)
import           Control.Monad.Trans.Resource (runResourceT)
import           Data.Conduit                 (($$))
import qualified Data.Conduit.Binary          as CB
import qualified Data.Conduit.List            as CL
import           Network.HTTP.Simple
import           System.IO                    (stdout)

main :: IO ()
main =
    runResourceT
        $ httpSource "http://httpbin.org/robots.txt" getSrc
       $$ CB.sinkHandle stdout
  where
    getSrc res = do
        liftIO $ print (getResponseStatus res, getResponseHeaders res)
        getResponseBody res

Types

10 declarations
typetype Header = (HeaderName, ByteString)
#

A full HTTP header field with the name and value separated.

E.g. "Content-Length: 28" parsed into a Header would turn into ("Content-Length", "28")

datadata 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

Instances2Show, IsString
datadata Response body
#

A simple representation of the HTTP response.

Since 0.1.0

Instances4Functor, Foldable, Traversable, Show
datadata HttpException
#

An exception which may be generated by this library

Constructors

Instances2Show, Exception
datadata Proxy
#

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

Constructors

Instances4Eq, Ord, Read, Show
  • Eq ProxyDefined in http-client-0.7.19 · Network.HTTP.Client.Types
  • Ord ProxyDefined in http-client-0.7.19 · Network.HTTP.Client.Types
  • Read ProxyDefined in http-client-0.7.19 · Network.HTTP.Client.Types
  • Show ProxyDefined in http-client-0.7.19 · Network.HTTP.Client.Types

Request constructions

5 declarations
valueparseRequest :: MonadThrow m => String -> m Request
#

Convert a URL into a Request.

This function 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.:

parseRequest "POST http://httpbin.org/post"

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

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

To create a request which throws on non-2XX status codes, see parseUrlThrow

Same as parseRequest, except will throw an HttpException in the event of a non-2XX response. This uses throwErrorStatusCodes to implement checkResponse.

Exactly the same as parseUrlThrow, but has a name that is more consistent with the other parseRequest functions.

Request lenses

0 declarations

Basics

Request body

Set the request body to the given RequestBody. You may want to consider using one of the convenience functions in the modules, e.g. requestBodyJSON.

Note: This will not modify the request method. For that, please use requestMethod. You likely don't want the default of GET.

valuesetRequestBodyJSON :: ToJSON a => a -> Request -> Request
#

Set the request body as a JSON value

Note: This will not modify the request method. For that, please use requestMethod. You likely don't want the default of GET.

This also sets the Content-Type to application/json; charset=utf-8

NOTE: Depends on the aeson cabal flag being enabled

Set the request body as a lazy ByteString

Note: This will not modify the request method. For that, please use requestMethod. You likely don't want the default of GET.

Set the request body as a file

Note: This will not modify the request method. For that, please use requestMethod. You likely don't want the default of GET.

Special fields

Response lenses

5 declarations

Alternate spellings

1 declaration