1 package Catalyst::ActionRole::ConsumesContent;
5 requires 'match', 'match_captures';
7 has allowed_content_types => (
13 builder=>'_build_allowed_content_types');
20 builder=>'_build_normalized');
23 sub _build_normalized {
25 JSON => 'application/json',
26 JS => 'application/javascript',
27 PERL => 'application/perl',
30 Plain => 'text/plain',
31 UrlEncoded => 'application/x-www-form-urlencoded',
32 Multipart => 'multipart/form-data',
33 HTMLForm => ['application/x-www-form-urlencoded','multipart/form-data'],
37 sub _build_allowed_content_types {
39 my @proto = split ',', @{$self->attributes->{Consumes}};
41 if(my $normalized = $self->normalized->{$_}) {
42 ref $normalized ? @$normalized : ($normalized);
49 around ['match','match_captures'] => sub {
50 my ($orig, $self, $ctx, @args) = @_;
51 if(my $content_type = $ctx->req->content_type) {
52 return 0 unless $self->can_consume($content_type);
54 return $self->$orig($ctx, @args);
58 my ($self, $request_content_type) = @_;
59 my @matches = grep { lc($_) eq lc($request_content_type) }
60 $self->allowed_content_types;
61 return @matches ? 1:0;
68 Catalyst::ActionRole::ConsumesContent - Match on HTTP Request Content-Type
72 package MyApp::Web::Controller::MyController;
74 use base 'Catalyst::Controller';
76 sub start : POST Chained('/') CaptureArg(0) { ... }
78 sub is_json : Chained('start') Consumes('application/json') { ... }
79 sub is_urlencoded : Chained('start') Consumes('application/x-www-form-urlencoded') { ... }
80 sub is_multipart : Chained('start') Consumes('multipart/form-data') { ... }
82 ## Alternatively, for common types...
84 sub is_json : Chained('start') Consume(JSON) { ... }
85 sub is_urlencoded : Chained('start') Consume(HTMLForm)URLEncoded { ... }
86 sub is_multipart : Chained('start') ConsumeFormData { ... }
88 ## Or allow more than one type
92 : Consumes('application/x-www-form-urlencoded')
93 : Consumes('multipart/form-data')
102 This is an action role that lets your L<Catalyst::Action> match on the content
103 type of the incoming request.
105 Generally when there's a PUT or POST request, there's a request content body
106 with a matching MIME content type. Commonly this will be one of the types
107 used with classic HTML forms ('application/x-www-form-urlencoded' for example)
108 but there's nothing stopping you specifying any valid content type.
110 For matching purposes, we match strings but the casing is insensitive.
114 This role requires the following methods in the consuming class.
118 =head2 match_captures
120 Returns 1 if the action matches the existing request and zero if not.
124 This role defines the following methods
128 =head2 match_captures
130 Around method modifier that return 1 if the request content type matches one of the
131 allowed content types (see L</http_methods>) and zero otherwise.
133 =head2 allowed_content_types
135 An array of strings that are the allowed content types for matching this action.
139 Boolean. Does the current request match content type with what this actionrole
144 Catalyst Contributors, see Catalyst.pm
148 This library is free software. You can redistribute it and/or modify it under
149 the same terms as Perl itself.