Options for configuring a client.
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 declarationsA 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"
A lens onto proxy configuration.
Example:
let opts = defaults Control.Lens.& proxy Control.Lens.?~ httpProxy "localhost" 8000
getWith opts "http://httpbin.org/get"
Note here the use of the Control.Lens.?~ setter to turn a Proxy
into a Maybe Proxy, to make the type of the RHS compatible with
the proxy lens.
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"
A lens onto all query parameters.
A traversal onto the cookie with the given name, if one exists.
N.B. This is an "illegal" Traversal': we can change the cookieName of the associated Cookie so that it differs from the name provided to this function.
A lens onto all cookies.
A function that checks the result of a HTTP request and potentially throw an exception.
A lens to get the optional status check function
Proxy setup
Define a HTTP proxy, consisting of a hostname and port number.
A lens onto the hostname portion of a proxy configuration.
A lens onto the TCP port number of a proxy configuration.
Cookie
12 declarationsA lens onto the name of a cookie.
A lens onto the value of a cookie.
A lens onto the expiry time of a cookie.
A lens onto the domain of a cookie.
A lens onto the path of a cookie.
A lens onto the creation time of a cookie.
A lens onto the last access time of a cookie.
A lens onto whether a cookie is persistent across sessions (also known as a "tracking cookie").
A lens onto whether a cookie is host-only.
A lens onto whether a cookie is secure-only, such that it will only be used over TLS.
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 declarationsA simple representation of the HTTP response.
Since 0.1.0
Instances4Functor, Foldable, Traversable, Show
Functor ResponseDefined in http-client-0.7.19 · Network.HTTP.Client.TypesFoldable ResponseDefined in http-client-0.7.19 · Network.HTTP.Client.TypesTraversable ResponseDefined in http-client-0.7.19 · Network.HTTP.Client.TypesShow body => Show (Response body)Defined in http-client-0.7.19 · Network.HTTP.Client.Types
A lens onto the body of a response.
r <- get "http://httpbin.org/get"
print (r Control.Lens.^. responseBody)
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")
responseLink :: ByteStringParameter name to match.
-> ByteStringParameter value to match.
-> Fold (Response body) Link
A fold over Link headers, matching on both parameter name
and value.
For example, here is a Link header returned by the GitHub search API.
Link:
<https://api.github.com/search/code?q=addClass+user%3Amozilla&page=2>; rel="next",
<https://api.github.com/search/code?q=addClass+user%3Amozilla&page=34>; rel="last"And here is an example of how we can retrieve the URL for the next link
programatically.
r <- get "https://api.github.com/search/code?q=addClass+user:mozilla"
print (r Control.Lens.^? responseLink "rel" "next" . linkURL)
A fold over any cookies that match the given name.
r <- get "http://www.nytimes.com/"
print (r Control.Lens.^? responseCookie "RMID")
A lens onto all headers in an HTTP response.
A lens onto all cookies set in the response.
A lens onto the status of an HTTP response.
A lens onto the version of an HTTP response.
HistoriedResponse
4 declarationsA datatype holding information on redirected requests and the final response.
Since 0.4.1
Instances6Functor, Foldable, Traversable, Show, Generic, Rep
Functor HistoriedResponseDefined in http-client-0.7.19 · Network.HTTP.ClientFoldable HistoriedResponseDefined in http-client-0.7.19 · Network.HTTP.ClientTraversable HistoriedResponseDefined in http-client-0.7.19 · Network.HTTP.ClientShow body => Show (HistoriedResponse body)Defined in http-client-0.7.19 · Network.HTTP.ClientGeneric (HistoriedResponse body)Defined in http-client-0.7.19 · Network.HTTP.Clienttype Rep (HistoriedResponse body) = D1 ('MetaDataDefined in http-client-0.7.19 · Network.HTTP.Client"HistoriedResponse"
"Network.HTTP.Client"
"http-client-0.7.19-HrfOahD9xgrKGFtDZYGgUO"
'False) (C1 ('MetaCons"HistoriedResponse"
'PrefixI 'True) (S1 ('MetaSel ('Just"hrRedirects"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 [(Request, Response ByteString)]) :*: (S1 ('MetaSel ('Just"hrFinalRequest"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Request) :*: S1 ('MetaSel ('Just"hrFinalResponse"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (Response body)))))
A lens onto the final response of a historied response.
A lens onto the final request of a historied response.
A lens onto the list of redirects of a historied response.
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, …
Bounded StatusDefined in http-types-0.12.4 · Network.HTTP.Types.StatusEnum StatusDefined in http-types-0.12.4 · Network.HTTP.Types.StatusBe advised, that when using the "enumFrom*" family of methods or ranges in lists, it will generate all possible status codes.
E.g.
[status100 .. status200]generates Statuses of100, 101, 102 .. 198, 199, 200The statuses not included in this library will have an empty message.
Eq StatusDefined in http-types-0.12.4 · Network.HTTP.Types.StatusData StatusDefined in http-types-0.12.4 · Network.HTTP.Types.StatusOrd StatusDefined in http-types-0.12.4 · Network.HTTP.Types.StatusStatuses are ordered according to their status codes only.
Show StatusDefined in http-types-0.12.4 · Network.HTTP.Types.StatusGeneric StatusDefined in http-types-0.12.4 · Network.HTTP.Types.Statustype Rep Status = D1 ('MetaDataDefined in http-types-0.12.4 · Network.HTTP.Types.Status"Status"
"Network.HTTP.Types.Status"
"http-types-0.12.4-KkMxTblWZY5WJ4M2x8W7s"
'False) (C1 ('MetaCons"Status"
'PrefixI 'True) (S1 ('MetaSel ('Just"statusCode"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Int) :*: S1 ('MetaSel ('Just"statusMessage"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 ByteString)))
A lens onto the numeric identifier of an HTTP status.
A lens onto the textual description of an HTTP status.
Link header
3 declarationsAn element of a Link header.
A lens onto the URL portion of a Link element.
A lens onto the parameters of a Link element.
POST body part
5 declarationsInstances6Postable, Patchable, Putable
Postable PartDefined in wreq-0.5.4.3 · Network.Wreq.Types · orphanPatchable PartDefined in wreq-0.5.4.3 · Network.Wreq.Types · orphanPutable PartDefined in wreq-0.5.4.3 · Network.Wreq.Types · orphanPostable [Part]Defined in wreq-0.5.4.3 · Network.Wreq.Types · orphanPatchable [Part]Defined in wreq-0.5.4.3 · Network.Wreq.Types · orphanPutable [Part]Defined in wreq-0.5.4.3 · Network.Wreq.Types · orphan
A lens onto the name of the input element associated with
part of a multipart form upload.
A lens onto the filename associated with part of a multipart form upload.
A lens onto the content-type associated with part of a multipart form upload.
A lens onto the code that fetches the data associated with part of a multipart form upload.
Parsing
2 declarationsTurn 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.
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"]
The same as atto, but ensures that the parser consumes the entire input.
Equivalent to:
atto_ myParser = atto (myParser <* endOfInput)