respond-1.1.0: process and route HTTP requests and generate responses on top of WAI

Safe HaskellNone




types and tools for making responses



type Responder = Response -> IO ResponseReceived Source

the type of the responder callback that is handed to a WAI Application

response bodies.

type ResponseBody = (MediaType, ByteString) Source

contains both the content type header value and the body.

class ToResponseBody a where Source

instances of this class produce content-negotiated response bodies.

if you're in a situation where there are performance concerns around building a lazy bytestring for the response, you should consider instead building Responses by hand (i.e. by using the WAI functions).


toResponseBody Source


:: a

the value to convert

-> ByteString

the http accept header. will be "*/*" if the client didn't set it.

-> Maybe ResponseBody

content type header, body

specify the conversion. this is supposed to fail if and only if there is no way for the instance to satisfy the accept header. otherwise. a successful conversion will include both a bytestring to use as the body and the value to use for the content type header

tools to build instances

type MediaTypeMatcher a = (MediaType, a -> ByteString) Source

pair of media type to match and a function that produces a builder for a value

prepMediaTypeMatcher :: a -> MediaTypeMatcher a -> (MediaType, ResponseBody) Source

converts a media type matcher into a pair of media type and response body...

yes, what it does is duplicate the media type and apply the builder to the value

matchToContentTypes :: [MediaTypeMatcher a] -> a -> ByteString -> Maybe ResponseBody Source

find the media type matcher that matches the passed Accept header value, and use it to produce a ResponseBuilder for the passed value. fail with Nothing if no builder can be found given the header. see prepMediaTypeMatcher

matchToContentTypesDefault :: MediaTypeMatcher a -> [MediaTypeMatcher a] -> a -> ByteString -> ResponseBody Source

try to match using matchToContents; if that fails, use the response body generated by the defaul matcher (first input)

charsetUtf8 :: (ByteString, ByteString) Source

the content type parameter charset=utf-8

textUtf8 :: MediaType -> (a -> Text) -> MediaTypeMatcher a Source

takes a text producer and produces a media type matcher that'll encode the text as utf-8 and annotate the content type with that parameter

you can use this in matchToContentTypes and render your type into lazy Text; this will make sure it gets encoded.

working with instances

mkResponseForBody :: Status -> ResponseHeaders -> ResponseBody -> Response Source

take a ResponseBody instance and turn it into a response

mkResponse :: ToResponseBody a => Status -> ResponseHeaders -> a -> ByteString -> Maybe Response Source

try to produce a Response object for an instance of ToResponseBody. the last input must be the value of the accept header

working with content types


jsonMatcher :: ToJSON a => MediaTypeMatcher a Source

make a MediaTypeMatcher that produces JSON

matchAcceptJson :: ToJSON a => a -> ByteString -> Maybe ResponseBody Source

matches only if client accepts "application/json"

renderable content types


htmlMatcher :: (a -> ByteString) -> MediaTypeMatcher a Source

builds an html matcher

matchAcceptHtml :: (a -> ByteString) -> a -> ByteString -> Maybe ResponseBody Source

matches only html acceptance