webgear-core-1.2.0: Composable, type-safe library to build HTTP APIs
Safe HaskellSafe-Inferred
LanguageHaskell2010

WebGear.Core.Trait.Body

Description

Traits and middlewares to handle request and response body payloads.

The requestBody middleware attempts to convert the body to a Haskell value or invoke an error handler if that fails.

The respondA middleware generates a response from an HTTP status and a response body.

If you need finer control over setting the body, use setBody or setBodyWithoutContentType. These arrows accept a witnessed 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

Traits

newtype Body (mimeType :: Type) (t :: Type) Source #

Request or response body with MIME types mimeTypes and type t.

Constructors

Body mimeType 

Instances

Instances details
Trait (Body mt t) Request Source # 
Instance details

Defined in WebGear.Core.Trait.Body

Associated Types

type Attribute (Body mt t) Request Source #

Trait (Body mt t) Response Source # 
Instance details

Defined in WebGear.Core.Trait.Body

Associated Types

type Attribute (Body mt t) Response Source #

TraitAbsence (Body mt t) Request Source # 
Instance details

Defined in WebGear.Core.Trait.Body

Associated Types

type Absence (Body mt t) Request Source #

type Absence (Body mt t) Request Source # 
Instance details

Defined in WebGear.Core.Trait.Body

type Absence (Body mt t) Request = Text
type Attribute (Body mt t) Request Source # 
Instance details

Defined in WebGear.Core.Trait.Body

type Attribute (Body mt t) Request = t
type Attribute (Body mt t) Response Source # 
Instance details

Defined in WebGear.Core.Trait.Body

type Attribute (Body mt t) Response = t
type Prerequisite (Body mt t) ts Request Source # 
Instance details

Defined in WebGear.Core.Trait.Body

type Prerequisite (Body mt t) ts Request = ()

data UnknownContentBody Source #

Type representing responses without a statically known MIME type

Constructors

UnknownContentBody 

Middlewares

requestBody Source #

Arguments

:: forall t mt h m ts. (Handler h m, Get h (Body mt t) Request) 
=> mt 
-> h (Request `With` ts, Text) Response

Error handler in case body cannot be retrieved

-> Middleware h ts (Body mt t : ts) 

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 @Text PlainText errorHandler nextHandler

respondA Source #

Arguments

:: forall body mt h m. (Handler h m, Sets h [Status, Body mt body, RequiredResponseHeader "Content-Type" Text] Response, MIMEType mt) 
=> Status

Response status

-> mt 
-> h body Response 

A convenience arrow to generate a response specifying a status and body.

The "Content-Type" header will be set to the value specified by mt.

Usage:

 let body :: SomeJSONType = ...
 respondA ok200 JSON -< body

setBody :: forall body mt h ts. (Sets h [Body mt body, RequiredResponseHeader "Content-Type" Text] Response, MIMEType mt) => mt -> h (Response `With` ts, body) (Response `With` (Body mt body : (RequiredResponseHeader "Content-Type" Text : ts))) Source #

Set the response body along with a media type.

The MIME type mt is used to set the "Content-Type" header in the response.

Usage:

 let body :: SomeJSONType = ...
 response' <- setBody JSON -< (response, body)

setBodyWithoutContentType :: forall h ts. Set h UnknownContentBody Response => h (Response `With` ts, ResponseBody) (Response `With` (UnknownContentBody : ts)) Source #

Set the response body without specifying any media type.

Usage:

 let body :: ResponseBody = ...
 response' <- setBodyWithoutContentType -< (response, body)