webgear-core-1.3.1: Composable, type-safe library to build HTTP APIs
Safe HaskellNone
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 t Source #

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

Constructors

Body mimeType 

Instances

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

Defined in WebGear.Core.Trait.Body

type Absence (Body mt t) = 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 Source # 
Instance details

Defined in WebGear.Core.Trait.Body

type Prerequisite (Body mt t) ts = ()

data UnknownContentBody Source #

Type representing responses without a statically known MIME type

Constructors

UnknownContentBody 

Instances

Instances details
type Attribute UnknownContentBody Response Source # 
Instance details

Defined in WebGear.Core.Trait.Body

Middlewares

requestBody Source #

Arguments

:: forall t mt h (m :: Type -> Type) (ts :: [Type]). (Handler h m, Get h (Body mt t)) 
=> mt 
-> h (With Request 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 :: Type -> Type). (Handler h m, Sets h '[Status, Body mt body, RequiredResponseHeader "Content-Type" Text], 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 :: [Type]). (Sets h '[Body mt body, RequiredResponseHeader "Content-Type" Text], MIMEType mt) => mt -> h (With Response ts, body) (With Response (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 :: [Type]). Set h UnknownContentBody => h (With Response ts, ResponseBody) (With Response (UnknownContentBody ': ts)) Source #

Set the response body without specifying any media type.

Usage:

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