Perform an HTTP request and return the body as a ByteString.
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 . getResponseBodyThe IsString instance uses parseRequest behind the scenes and inherits its behavior.
- 10 types
- 44 values
- Packagehttp-conduit-2.3.9.1
- Exports54
- LanguageHaskell2010
- LicenceBSD-3-Clause
- SourceSimple.hs
Perform requests
8 declarationsPerform 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.
Perform an HTTP request and ignore the response body.
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
Perform an HTTP request and parse the body as JSON. In the event of an
JSON parse errors, a Left value will be returned.
NOTE: Depends on the aeson cabal flag being enabled
Perform an HTTP request and consume the body with the given Sink
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
Perform an action with the given request. This employes the bracket pattern.
This is similar to httpSource, but does not require MonadResource and allows the result to not contain a ConduitM value.
Types
10 declarationsA 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")
A sequence of QueryItems.
An item from the query string, split up into two parts.
The second part should be Nothing if there was no key-value separator after the query item name.
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
Show RequestDefined in http-client-0.7.19 · Network.HTTP.Client.TypesIsString RequestDefined in http-client-0.7.19 · Network.HTTP.Client.Request · orphanParses a URL via parseRequest_
NOTE: Prior to version 0.5.0, this instance used parseUrlThrow instead.
A list of Headers.
Same type as ResponseHeaders, but useful to differentiate in type signatures.
A 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 list of Headers.
Same type as RequestHeaders, but useful to differentiate in type signatures.
An exception that can occur when parsing JSON
NOTE: Depends on the aeson cabal flag being enabled
Constructors
Instances2Show, Exception
Show JSONExceptionDefined in http-conduit-2.3.9.1 · Network.HTTP.SimpleException JSONExceptionDefined in http-conduit-2.3.9.1 · Network.HTTP.Simple
An exception which may be generated by this library
Constructors
HttpExceptionRequest Request HttpExceptionContentMost exceptions are specific to a Request. Inspect the HttpExceptionContent value for details on what occurred.
InvalidUrlException String StringA URL (first field) is invalid for a given reason (second argument).
Instances2Show, Exception
Show HttpExceptionDefined in http-client-0.7.19 · Network.HTTP.Client.TypesException HttpExceptionDefined in http-client-0.7.19 · Network.HTTP.Client.Types
Define a HTTP proxy, consisting of a hostname and port number.
Constructors
ProxyproxyHost :: ByteStringThe host name of the HTTP proxy in URI format. IPv6 addresses in square brackets.
proxyPort :: IntThe port number of the HTTP proxy.
Request constructions
5 declarationsA default request value, a GET request of localhost/:80, with an empty request body.
Note that the default checkResponse does nothing.
Convert a URL into a Request.
This function defaults some of the values in Request, such as setting method to
and requestHeaders to GET[].
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, but parse errors cause an impure exception. Mostly useful for static strings which are known to be correctly formatted.
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.
Same as parseRequestThrow, but parse errors cause an impure exception. Mostly useful for static strings which are known to be correctly formatted.
Request lenses
0 declarationsBasics
Set the request method
Set whether this is a secureHTTPS (True) or insecureHTTP
(False) request
Set the destination host of the request
Set the destination port of the request
Lens for the requested path info of the request
Add a request header name/value combination
Get all request header values for the given name
Set the given request header to the given list of values. Removes any previously set header values with the same name.
Set the request headers, wiping out all previously set headers. This
means if you use setRequestHeaders to set some headers and also use one of
the other setters that modifies the content-type header (such as
setRequestBodyJSON), be sure that setRequestHeaders is evaluated
first.
Set the query string parameters
Get the query string parameters
Add to the existing query string parameters.
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.
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.
setRequestBodySource Set the request body as a Source
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.
Set the request body as URL encoded data
Note: This will change the request method to POST and set the content-type
to application/x-www-form-urlencoded
Special fields
Modify the request so that non-2XX status codes do not generate a runtime StatusCodeException.
Modify the request so that non-2XX status codes generate a runtime StatusCodeException, by using throwErrorStatusCodes
Set basic auth with the given username and password
Set bearer auth with the given token
Instead of using the default global Manager, use the supplied
Manager.
Override the default proxy server settings
Set the maximum time to wait for a response
Response lenses
5 declarationsGet the status of the response
Get the integral status code of the response
Get all response header values with the given name
Get all response headers
Get the response body
Alternate spellings
1 declarationAlternate spelling of httpLBS