Rob Hoelz

NAME

Plack::Middleware::SetAccept - Sets the Accept header based on the suffix or query params of a request

VERSION

version 0.01

SYNOPSIS

  use Plack::Builder;

  my %map = (
    json => 'application/json',
    xml  => 'application/xml',
  );

  builder {
    enable 'SetAccept', from => 'suffix', mapping => \%map;
    $app;
  };
  # now /foo.json behaves as /foo, with Accept: application/json

  # or

  builder {
    enable 'SetAccept', from => 'param', param => 'format', mapping => \%map;
    $app;
  };
  # now /foo?format=xml behaves as /foo, with Accept: application/xml

  # or
  
  builder {
    enable 'SetAccept', from => ['suffix', 'param'], param => 'format', mapping => \%map;
    $app;
  };

DESCRIPTION

This middleware sets the Accept header by extracting a piece of the request URI. It can extract from either the suffix of the path (ex. /foo.json) or from the query string (ex. /foo?format=json) for HEAD and GET requests. The value is looked up in a mapping table and is added to the Accept header.

PARAMETERS

from

Specifies from where the middleware is to extract the accept string. Valid values for this are 'suffix', 'param', or an array reference containing either/both of those values. The order in the array reference doesn't really matter, except for when the middleware generates XHTML links on a 406 error.

param

Only required when using 'param' for from. Specifies the query string parameter that specifies the lookup value for the mapping table.

mapping

A hash table containing Accept mappings. The keys should be the possible values extracted from the URI, and the values should be the mime types associated with the keys.

tolerant

If this option is falsy (defaults to 1), a 406 response code will be generated for "unacceptable" values. The body of the response will contain an XHTML document with a list of alternative links.

SEE ALSO

Plack, Plack::Middleware

AUTHOR

Rob Hoelz <rob@hoelz.ro>

COPYRIGHT AND LICENSE

This software is copyright (c) 2011 by Rob Hoelz.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

BUGS

Please report any bugs or feature requests on the bugtracker website http://github.com/hoelzro/plack-middleware-setaccept/issues

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.