Safe Haskell | None |
---|---|
Language | Haskell2010 |
Making HTTP requests using an API inspired by Elm's elm/http.
Synopsis
- data Handler
- handler :: Acquire Handler
- get :: Typeable a => Handler -> Text -> Expect a -> Task Error a
- post :: Typeable a => Handler -> Text -> Body -> Expect a -> Task Error a
- request :: Typeable expect => Handler -> Request expect -> Task Error expect
- data Request a = Request {}
- data Error
- = BadUrl Text
- | Timeout
- | NetworkError Text
- | BadStatus Int
- | BadBody Text
- data Header
- header :: Text -> Text -> Header
- data Body
- emptyBody :: Body
- stringBody :: Text -> Text -> Body
- jsonBody :: ToJSON body => body -> Body
- bytesBody :: Text -> ByteString -> Body
- data Expect a
- expectJson :: FromJSON a => Expect a
- expectText :: Expect Text
- expectWhatever :: Expect ()
- withThirdParty :: Handler -> (Manager -> Task e a) -> Task e a
- withThirdPartyIO :: LogHandler -> Handler -> (Manager -> IO a) -> IO a
Handlers
Requests
post :: Typeable a => Handler -> Text -> Body -> Expect a -> Task Error a Source #
Create a POST
request.
request :: Typeable expect => Handler -> Request expect -> Task Error expect Source #
Create a custom request.
A custom request.
Request | |
|
A Request
can fail in a couple of ways:
BadUrl
means you did not provide a valid URL.Timeout
means it took too long to get a response.NetworkError
means the user turned off their wifi, went in a cave, etc.BadStatus
means you got a response back, but the status code indicates failure.BadBody
means you got a response back with a nice status code, but the body of the response was something unexpected. TheText
in this cse is the debugging message that explains what went wrong with your JSONT decoder or whatever.
BadUrl Text | |
Timeout | |
NetworkError Text | |
BadStatus Int | |
BadBody Text |
Instances
Header,
An HTTP header for configuration requests.
Body
Create an empty body for your Request. This is useful for GET requests and POST requests where you are not sending any data.
stringBody :: Text -> Text -> Body Source #
Put some string in the body of your Request.
The first argument is a MIME type of the body. Some servers are strict about this!
jsonBody :: ToJSON body => body -> Body Source #
Put some JSON value in the body of your Request. This will automatically add the Content-Type: application/json header.
bytesBody :: Text -> ByteString -> Body Source #
Put some Bytes in the body of your Request. This allows you to use ByteString to have full control over the binary representation of the data you are sending.
The first argument is a MIME type of the body. In other scenarios you may want to use MIME types like imagepng or imagejpeg instead.
Expect
expectJson :: FromJSON a => Expect a Source #
Expect the response body to be JSON.
expectText :: Expect Text Source #
Expect the response body to be a Text
.
expectWhatever :: Expect () Source #
Expect the response body to be whatever. It does not matter. Ignore it!
Use with external libraries
withThirdParty :: Handler -> (Manager -> Task e a) -> Task e a Source #
Third party libraries that make HTTP requests often take a Manager
.
This helper allows us to call such a library using a Handler
.
The benefit over using this over using a separate Manager
for the
external library, is that withThirdParty
will ensure HTTP requests made
by the external library will get logged.
withThirdPartyIO :: LogHandler -> Handler -> (Manager -> IO a) -> IO a Source #
Like withThirdParty
, but runs in IO
.