1 package Catalyst::ActionRole::ConsumesContent;
5 requires 'match', 'match_captures', 'list_extra_info';
7 has allowed_content_types => (
12 builder=>'_build_allowed_content_types');
19 builder=>'_build_normalized');
22 sub _build_normalized {
24 JSON => 'application/json',
25 JS => 'application/javascript',
26 PERL => 'application/perl',
29 Plain => 'text/plain',
30 UrlEncoded => 'application/x-www-form-urlencoded',
31 Multipart => 'multipart/form-data',
32 HTMLForm => ['application/x-www-form-urlencoded','multipart/form-data'],
36 sub _build_allowed_content_types {
38 my @proto = map {split ',', $_ } @{$self->attributes->{Consumes}};
40 if(my $normalized = $self->normalized->{$_}) {
41 ref $normalized ? @$normalized : ($normalized);
50 around ['match','match_captures'] => sub {
51 my ($orig, $self, $ctx, @args) = @_;
52 if(my $content_type = $ctx->req->content_type) {
53 return 0 unless $self->can_consume($content_type);
55 return $self->$orig($ctx, @args);
59 my ($self, $request_content_type) = @_;
60 my @matches = grep { lc($_) eq lc($request_content_type) }
61 @{$self->allowed_content_types};
62 return @matches ? 1:0;
65 around 'list_extra_info' => sub {
66 my ($orig, $self, @args) = @_;
68 %{ $self->$orig(@args) },
69 CONSUMES => $self->allowed_content_types,
77 Catalyst::ActionRole::ConsumesContent - Match on HTTP Request Content-Type
81 package MyApp::Web::Controller::MyController;
83 use base 'Catalyst::Controller';
85 sub start : POST Chained('/') CaptureArg(0) { ... }
87 sub is_json : Chained('start') Consumes('application/json') { ... }
88 sub is_urlencoded : Chained('start') Consumes('application/x-www-form-urlencoded') { ... }
89 sub is_multipart : Chained('start') Consumes('multipart/form-data') { ... }
91 ## Alternatively, for common types...
93 sub is_json : Chained('start') Consume(JSON) { ... }
94 sub is_urlencoded : Chained('start') Consumes(UrlEncoded) { ... }
95 sub is_multipart : Chained('start') Consumes(Multipart) { ... }
97 ## Or allow more than one type
101 : Consumes('application/x-www-form-urlencoded')
102 : Consumes('multipart/form-data')
111 This is an action role that lets your L<Catalyst::Action> match on the content
112 type of the incoming request.
114 Generally when there's a PUT or POST request, there's a request content body
115 with a matching MIME content type. Commonly this will be one of the types
116 used with classic HTML forms ('application/x-www-form-urlencoded' for example)
117 but there's nothing stopping you specifying any valid content type.
119 For matching purposes, we match strings but the casing is insensitive.
123 This role requires the following methods in the consuming class.
127 =head2 match_captures
129 Returns 1 if the action matches the existing request and zero if not.
133 This role defines the following methods
137 =head2 match_captures
139 Around method modifier that return 1 if the request content type matches one of the
140 allowed content types (see L</http_methods>) and zero otherwise.
142 =head2 allowed_content_types
144 An array of strings that are the allowed content types for matching this action.
148 Boolean. Does the current request match content type with what this actionrole
151 =head2 list_extra_info
153 Add the accepted content type to the debug screen.
157 Catalyst Contributors, see Catalyst.pm
161 This library is free software. You can redistribute it and/or modify it under
162 the same terms as Perl itself.