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

Modulewreq-0.5.4.3Haskell98

Network.Wreq.Lens

HTTP client lens machinery.

When reading the examples in this module, you should assume the following environment:

-- Make it easy to write literal S.ByteString and Text values.
{-# LANGUAGE OverloadedStrings #-}

-- Our handy module.
import Network.Wreq

-- Operators such as (&) and (.~).
import Control.Lens

-- Conversion of Haskell values to JSON.
import Data.Aeson (Data.Aeson.toJSON)

-- Easy traversal of JSON data.
import Data.Aeson.Lens (key, nth)
  • 9 types
  • 45 values
  • Packagewreq-0.5.4.3
  • Exports54
  • LanguageHaskell98
  • LicenceBSD-3-Clause
  • SourceLens.hs

Configuration

13 declarations
datadata Options
#

Options for configuring a client.

Instances1Show
  • Show OptionsDefined in wreq-0.5.4.3 · Network.Wreq.Internal.Types

A lens onto configuration of the connection manager provided by the http-client package.

In this example, we enable the use of OpenSSL for (hopefully) secure connections:

import OpenSSL.Session (context)
import Network.HTTP.Client.OpenSSL

let opts = defaults Control.Lens.& manager Control.Lens..~ Left (Network.HTTP.Client.OpenSSL.opensslManagerSettings context)
Network.HTTP.Client.OpenSSL.withOpenSSL $
  getWith opts "https://httpbin.org/get"
 

In this example, we also set the response timeout to 10000 microseconds:

import OpenSSL.Session (context)
import Network.HTTP.Client.OpenSSL
import Network.HTTP.Client (defaultManagerSettings, managerResponseTimeout)

let opts = defaults Control.Lens.& manager Control.Lens..~ Left (Network.HTTP.Client.OpenSSL.opensslManagerSettings context)
                    Control.Lens.& manager Control.Lens..~ Left (defaultManagerSettings { managerResponseTimeout = responseTimeoutMicro 10000 } )

Network.HTTP.Client.OpenSSL.withOpenSSL $
  getWith opts "https://httpbin.org/get"
 
valueauth :: Lens' Options (Maybe Auth)
#

A lens onto request authentication.

Example (note the use of TLS):

let opts = defaults Control.Lens.& Lens.auth Control.Lens.?~ basicAuth "user" "pass"
getWith opts "https://httpbin.org/basic-auth/user/pass"
 
valueparam :: Text -> Lens' Options [Text]
#

A lens onto all query parameters with the given name (there can legitimately be zero or more).

In this example, we construct the query URL "http://httpbin.org/get?foo=bar&foo=quux".

let opts = defaults Control.Lens.& param "foo" Control.Lens..~ ["bar", "quux"]
getWith opts "http://httpbin.org/get"
 

A lens onto the maximum number of redirects that will be followed before an exception is thrown.

In this example, a HttpException will be thrown with a TooManyRedirects constructor, because the maximum number of redirects allowed will be exceeded.

let opts = defaults Control.Lens.& redirects Control.Lens..~ 3
getWith opts "http://httpbin.org/redirect/5"
 
valueheaders :: Lens' Options [Header]
#

A lens onto all headers (there can legitimately be zero or more).

In this example, we print all the headers sent by default with every request.

print (defaults Control.Lens.^. headers)
 

Proxy setup

datadata Proxy
#

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

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

Cookie

12 declarations
datadata Cookie
#
Instances2Read, Show
  • Read CookieDefined in http-client-0.7.19 · Network.HTTP.Client.Types
  • Show CookieDefined in http-client-0.7.19 · Network.HTTP.Client.Types

A lens onto whether a cookie is "HTTP-only".

Such cookies should be used only by browsers when transmitting HTTP requests. They must be unavailable in non-browser environments, such as when executing JavaScript scripts.

Response

9 declarations
datadata Response body
#

A simple representation of the HTTP response.

Since 0.1.0

Instances4Functor, Foldable, Traversable, Show
valueresponseHeader
  1. :: HeaderName

    Header name to match.

  2. -> Traversal' (Response body) ByteString
#

A lens onto all matching named headers in an HTTP response.

To access exactly one header (the result will be the empty string if there is no match), use the (Control.Lens.^.) operator.

r <- get "http://httpbin.org/get"
print (r Control.Lens.^. responseHeader "Content-Type")
 

To access at most one header (the result will be Nothing if there is no match), use the (Control.Lens.^?) operator.

r <- get "http://httpbin.org/get"
print (r Control.Lens.^? responseHeader "Content-Transfer-Encoding")
 

To access all (zero or more) matching headers, use the (Control.Lens.^..) operator.

r <- get "http://httpbin.org/get"
print (r Control.Lens.^.. responseHeader "Set-Cookie")
 

HistoriedResponse

4 declarations
datadata HistoriedResponse body
#

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

Since 0.4.1

Instances6Functor, Foldable, Traversable, Show, Generic, Rep

Status

datadata Status
#

HTTP Status.

Only the statusCode is used for comparisons.

Please use mkStatus to create status codes from code and message, or the Enum instance or the status code constants (like ok200). There might be additional record members in the future.

Note that the Show instance is only for debugging.

Instances8Bounded, Enum, Eq, Data, Ord, Show, …

Link header

3 declarations

POST body part

5 declarations
typetype Part = PartM IO
#
Instances6Postable, Patchable, Putable
  • Postable PartDefined in wreq-0.5.4.3 · Network.Wreq.Types · orphan
  • Patchable PartDefined in wreq-0.5.4.3 · Network.Wreq.Types · orphan
  • Putable PartDefined in wreq-0.5.4.3 · Network.Wreq.Types · orphan
  • Postable [Part]Defined in wreq-0.5.4.3 · Network.Wreq.Types · orphan
  • Patchable [Part]Defined in wreq-0.5.4.3 · Network.Wreq.Types · orphan
  • Putable [Part]Defined in wreq-0.5.4.3 · Network.Wreq.Types · orphan

Parsing

2 declarations
valueatto :: Parser a -> Fold ByteString a
#

Turn an attoparsec Parser into a Fold.

Both headers and bodies can contain complicated data that we may need to parse.

Example: when responding to an OPTIONS request, a server may return the list of verbs it supports in any order, up to and including changing the order on every request (which httpbin.org /actually does/!). To deal with this possibility, we parse the list, then sort it.

Example8 expressions
import Data.Attoparsec.ByteString.Char8 as Aimport Data.List (sort)let comma = skipSpace >> "," >> skipSpacelet verbs = A.takeWhile isAlpha_ascii `sepBy` commar <- options "http://httpbin.org/get"r ^. responseHeader "Allow" . atto verbs . to sort["GET","HEAD","OPTIONS"]