Processing HTTP Content-Type and Accept headers

Version on this page:
LTS Haskell 22.30:
Stackage Nightly 2024-07-22:
Latest on Hackage:

See all snapshots http-media appears in

MIT licensed by Timothy Jones
Maintained by Timothy Jones
This version can be pinned in stack with:http-media-,4238

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:

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:

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

The API is agnostic to your choice of server.



  • Version

    Removed official support for GHC 7.8.

    A separate QualityOrder type can now be extracted from a Quality value for performing comparisons without the attached data value.

    The most specific match will now be used to assign a quality value to each server option, ensuring that less specific matches cannot override lower quality values on more specific matches. In particular, if a type is considered unacceptable by the client, then a separate match with a non-zero quality value cannot make it acceptable.

    Numeric characters are now permitted in the tail of a Language value.

    Added support for Accept-Encoding and Content-Encoding.

    Added support for Accept-Charset and Content-Charset.

    The bounds for QuickCheck have been updated to include the latest version.

  • Version

    Package bounds have been updated for GHC 8.6.

  • Version

    The bounds for QuickCheck have been updated to include the latest version.

  • Version

    The bounds for base have been updated to include support for GHC 8.2.

  • Version 0.7.1

    Travis now tests against a range of Stackage LTS environments, instead of using multi-ghc.

    Support for base-4.6 has now been correctly removed in the Cabal file.

  • Version 0.7.0

    The Travis configuration has dropped support for GHC 7.6 and added support for 8.0.

    More direct constructors for quality values are now available, to avoid having to deal with Maybe results when you are certain parsing a quality string will not fail.

    The bounds for QuickCheck have been updated to include the latest version.

  • Version 0.6.4

    The bounds for QuickCheck have been updated to include the latest version.

  • Version 0.6.3

    Parse failures more regularly return a Maybe value instead of raising an exception.

    The (//) smart constructor now accepts wildcard arguments, but only in the correct order.

    Most tests will now emit a counter example if their relevant properties are violated. Some tests which were not correctly covering their properties have been fixed.

    The -Werror flag has been removed from the test suite.

  • Version 0.6.2

    The test suite now uses the test-framework library instead of cabal-test-quickcheck, and the package no longer depends on Cabal.

  • Version 0.6.1

    The type errors and build warnings caused by the BBP have been fixed for GHC 7.10.

  • Version 0.6.0

    All of the publicly exposed data types now derive an Ord instance.