Skip to content
Processing HTTP Content-Type and Accept headers
Branch: develop
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src/Network/HTTP
test
.gitignore
.travis.yml
CHANGES.md
CONTRIBUTING.md
LICENSE
README.md
Setup.hs
http-media.cabal

README.md

http-media

Build Status

This library is intended to be a comprehensive solution to parsing and selecting quality-indexed values in HTTP headers. It is capable of parsing both media types and language parameters from the Accept and Content header families, and can be extended to match against other accept headers as well. Selecting the appropriate header value is achieved by comparing a list of server options against the quality-indexed values supplied by the client.

In the following example, the Accept header is parsed and then matched against a list of server options to serve the appropriate media using mapAcceptMedia:

send = getHeader >>= maybe send406Error sendResourceWith . mapAcceptMedia
    [ ("text/html",        asHtml)
    , ("application/json", asJson)
    ]

Similarly, the Content-Type header can be used to produce a parser for request bodies based on the given content type with mapContentMedia:

recv = getContentType >>= maybe send415Error readRequestBodyWith . mapContentMedia
    [ ("application/json", parseJson)
    , ("text/plain",       parseText)
    ]

The API is agnostic to your choice of server.

You can’t perform that action at this time.