[Catalyst] Forcing charset=utf-8 on all Content-Types for Catalyst::Controller::REST

Larry Leszczynski larryl at emailplus.org
Mon Jul 26 15:36:11 GMT 2010


Hi Dave -

> After searching google, and reading through all of the relevant
> documentation, and a considerable portion of the code, we have been
> unable to locate the correct place to set or override the Content-Type
> header to append a "charset=utf-8" string, required by our caching proxy
> to properly handle the encoding.  We would like a place to handle all
> mimetypes, so that our JSON, JSONP, and XML interfaces all get the
> correct charset set.

If you're sure all your content actually is utf-8, one way is to
subclass Catalyst::View and do something like:

after process => sub {

    my ($self, $c) = @_;

    my $ct = $c->response->content_type;

    if ($ct)
    {
        $ct =~ s/;\s*charset=.*//i;
        $ct .= '; charset=utf-8';
    }
    else
    {
        $ct = 'text/html; charset=utf-8';
    }

    $c->response->content_type($ct);
};


Thanks!
Larry



More information about the Catalyst mailing list