Author image Rob Hoelz
and 1 contributors


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


version 0.01


  use Plack::Builder;

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

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

  # or

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

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


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.



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.


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


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.


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.


Plack, Plack::Middleware


Rob Hoelz <>


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.


Please report any bugs or feature requests on the bugtracker website

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.