Safe Haskell | None |
---|---|
Language | Haskell2010 |
Traits and middlewares to handle request and response body payloads.
There are a number of ways to extract a body from a request:
The requestBody
middleware attempts to convert the body to a
Haskell value or invoke an error handler if that fails.
The jsonRequestBody
middleware attempts to convert a JSON
formatted body to a Haskell value or invoke an error handler if that
fails. It uses the standard "application/json" media type.
The jsonRequestBody'
middleware is similar but supports custom
media types.
Similarly, there are a number of ways to set a response body:
The easiest option is to use one of respondA
, respondJsonA
, or
respondJsonA'
middlewares. These middlewares generate a response
from an HTTP status and a response body.
If you need finer control over setting the body, use one of
setBody
, setBodyWithoutContentType
, setJSONBody
,
setJSONBodyWithoutContentType
, or setJSONBody'
. These arrows
accept a linked response and a body and sets the body in the
response. You can generate an input response object using functions
from WebGear.Core.Trait.Status module.
Synopsis
- newtype Body (t :: Type) = Body (Maybe MediaType)
- newtype JSONBody (t :: Type) = JSONBody (Maybe MediaType)
- requestBody :: forall t h req. (Get h (Body t) Request, ArrowChoice h) => Maybe MediaType -> h (Linked req Request, Text) Response -> Middleware h req (Body t ': req)
- jsonRequestBody' :: forall t h req. (Get h (JSONBody t) Request, ArrowChoice h) => Maybe MediaType -> h (Linked req Request, Text) Response -> Middleware h req (JSONBody t ': req)
- jsonRequestBody :: forall t h req. (Get h (JSONBody t) Request, ArrowChoice h) => h (Linked req Request, Text) Response -> Middleware h req (JSONBody t ': req)
- respondA :: forall body h. Sets h [Status, Body body, RequiredHeader "Content-Type" Text] Response => Status -> MediaType -> h body (Linked [RequiredHeader "Content-Type" Text, Body body, Status] Response)
- respondJsonA :: forall body h. Sets h [Status, JSONBody body, RequiredHeader "Content-Type" Text] Response => Status -> h body (Linked [RequiredHeader "Content-Type" Text, JSONBody body, Status] Response)
- respondJsonA' :: forall body h. Sets h [Status, JSONBody body, RequiredHeader "Content-Type" Text] Response => Status -> MediaType -> h body (Linked [RequiredHeader "Content-Type" Text, JSONBody body, Status] Response)
- setBody :: forall body a h ts. Sets h [Body body, RequiredHeader "Content-Type" Text] Response => MediaType -> h a (Linked ts Response) -> h (body, a) (Linked (RequiredHeader "Content-Type" Text ': (Body body ': ts)) Response)
- setBodyWithoutContentType :: forall body a h ts. Set h (Body body) Response => h a (Linked ts Response) -> h (body, a) (Linked (Body body ': ts) Response)
- setJSONBody :: forall body a h ts. Sets h [JSONBody body, RequiredHeader "Content-Type" Text] Response => h a (Linked ts Response) -> h (body, a) (Linked (RequiredHeader "Content-Type" Text ': (JSONBody body ': ts)) Response)
- setJSONBodyWithoutContentType :: forall body a h ts. Set h (JSONBody body) Response => h a (Linked ts Response) -> h (body, a) (Linked (JSONBody body ': ts) Response)
- setJSONBody' :: forall body a h ts. Sets h [JSONBody body, RequiredHeader "Content-Type" Text] Response => MediaType -> h a (Linked ts Response) -> h (body, a) (Linked (RequiredHeader "Content-Type" Text ': (JSONBody body ': ts)) Response)
Traits
newtype Body (t :: Type) Source #
Request or response body with a type t
.
Instances
TraitAbsence (Body t) Request Source # | |
Trait (Body t) Response Source # | |
Trait (Body t) Request Source # | |
type Absence (Body t) Request Source # | |
Defined in WebGear.Core.Trait.Body | |
type Attribute (Body t) Response Source # | |
Defined in WebGear.Core.Trait.Body | |
type Attribute (Body t) Request Source # | |
Defined in WebGear.Core.Trait.Body |
newtype JSONBody (t :: Type) Source #
A Trait
for converting a JSON formatted body into a value.
Instances
TraitAbsence (JSONBody t) Request Source # | |
Trait (JSONBody t) Response Source # | |
Trait (JSONBody t) Request Source # | |
type Absence (JSONBody t) Request Source # | |
Defined in WebGear.Core.Trait.Body | |
type Attribute (JSONBody t) Response Source # | |
Defined in WebGear.Core.Trait.Body | |
type Attribute (JSONBody t) Request Source # | |
Defined in WebGear.Core.Trait.Body |
Middlewares
:: forall t h req. (Get h (Body t) Request, ArrowChoice h) | |
=> Maybe MediaType | Optional media type of the body |
-> h (Linked req Request, Text) Response | Error handler in case body cannot be retrieved |
-> Middleware h req (Body t ': req) |
Middleware to extract a request body.
The nextHandler
is invoked after successfully extracting the body
and the errorHandler
is invoked when there is an error.
Usage:
requestBody @t (Just "text/plain") errorHandler nextHandler
:: forall t h req. (Get h (JSONBody t) Request, ArrowChoice h) | |
=> Maybe MediaType | Optional media type of the body |
-> h (Linked req Request, Text) Response | Error handler in case body cannot be retrieved |
-> Middleware h req (JSONBody t ': req) |
Parse the request body as JSON and convert it to a value of type
t
.
The nextHandler
is invoked when the body is parsed successfully and
the errorHandler
is invoked when there is a parsing failure.
Usage:
jsonRequestBody @t errorHandler nextHandler
:: forall t h req. (Get h (JSONBody t) Request, ArrowChoice h) | |
=> h (Linked req Request, Text) Response | error handler |
-> Middleware h req (JSONBody t ': req) |
Same as jsonRequestBody'
but with a media type application/json
.
:: forall body h. Sets h [Status, Body body, RequiredHeader "Content-Type" Text] Response | |
=> Status | Response status |
-> MediaType | Media type of the response body |
-> h body (Linked [RequiredHeader "Content-Type" Text, Body body, Status] Response) |
A convenience arrow to generate a response specifying a status and body.
The "Content-Type" header will be set to the specified media type value.
:: forall body h. Sets h [Status, JSONBody body, RequiredHeader "Content-Type" Text] Response | |
=> Status | Response status |
-> h body (Linked [RequiredHeader "Content-Type" Text, JSONBody body, Status] Response) |
A convenience arrow to generate a response specifying a status and JSON body.
The "Content-Type" header will be set to "application/json".
:: forall body h. Sets h [Status, JSONBody body, RequiredHeader "Content-Type" Text] Response | |
=> Status | Response status |
-> MediaType | Media type of the response body |
-> h body (Linked [RequiredHeader "Content-Type" Text, JSONBody body, Status] Response) |
A convenience arrow to generate a response specifying a status and JSON body.
The "Content-Type" header will be set to the specified media type value.
:: forall body a h ts. Sets h [Body body, RequiredHeader "Content-Type" Text] Response | |
=> MediaType | The media type of the response body |
-> h a (Linked ts Response) | |
-> h (body, a) (Linked (RequiredHeader "Content-Type" Text ': (Body body ': ts)) Response) |
Set the response body along with a media type.
The media type value is used to set the "Content-Type" header in the response.
setBodyWithoutContentType :: forall body a h ts. Set h (Body body) Response => h a (Linked ts Response) -> h (body, a) (Linked (Body body ': ts) Response) Source #
Set the response body without specifying any media type.
setJSONBody :: forall body a h ts. Sets h [JSONBody body, RequiredHeader "Content-Type" Text] Response => h a (Linked ts Response) -> h (body, a) (Linked (RequiredHeader "Content-Type" Text ': (JSONBody body ': ts)) Response) Source #
Set the response body to a JSON value.
The "Content-Type" header will be set to "application/json".
setJSONBodyWithoutContentType :: forall body a h ts. Set h (JSONBody body) Response => h a (Linked ts Response) -> h (body, a) (Linked (JSONBody body ': ts) Response) Source #
Set the response body to a JSON value without specifying any media type.
:: forall body a h ts. Sets h [JSONBody body, RequiredHeader "Content-Type" Text] Response | |
=> MediaType | The media type of the response body |
-> h a (Linked ts Response) | |
-> h (body, a) (Linked (RequiredHeader "Content-Type" Text ': (JSONBody body ': ts)) Response) |
Set the response body to a JSON value along with a media type.
The media type value is used to set the "Content-Type" header in the response.