次のようにrenderメソッドでformat設定してContent-Typeを指定できる。
$self->render(text => 'Hello!', format => 'txt');
デフォルトでformatに設定できるものは次のようになっている。
atom => 'application/atom+xml', bin => 'application/octet-stream', css => 'text/css', gif => 'image/gif', gz => 'application/gzip', htm => 'text/html', html => 'text/html', ico => 'image/x-icon', jpeg => 'image/jpeg', jpg => 'image/jpeg', js => 'application/x-javascript', json => 'application/json', mp3 => 'audio/mpeg', png => 'image/png', rss => 'application/rss+xml', svg => 'image/svg+xml', tar => 'application/x-tar', txt => 'text/plain', xml => 'text/xml', zip => 'application/zip'
独自に新しいformatも作成できる。
# Application package MyApp; use base 'Mojolicious'; sub startup { my $self = shift; # Add new MIME type $self->types->type(txt8 => 'text/plain; charset=utf-8'); } 1;
参考
- http://tinyurl.com/379ezm4
- perldoc -m MojoX::Types