1 package Catalyst::ActionRole::ConsumesContent;
5 requires 'match', 'match_captures';
7 has allowed_content_types => (
13 builder=>'_build_allowed_content_types');
15 sub _build_allowed_content_types { shift->attributes->{Consumes} }
17 around ['match','match_captures'] => sub {
18 my ($orig, $self, $ctx, @args) = @_;
19 if(my $content_type = $ctx->req->content_type) {
20 return 0 unless $self->can_consume($content_type);
22 return $self->$orig($ctx, @args);
26 my ($self, $request_content_type) = @_;
27 my @matches = grep { lc($_) eq lc($request_content_type) }
28 $self->allowed_content_types;
29 return @matches ? 1:0;
36 Catalyst::ActionRole::ConsumesContent - Match on HTTP Request Content-Type
40 package MyApp::Web::Controller::MyController;
42 use base 'Catalyst::Controller';
44 sub start : POST Chained('/') CaptureArg(0) { ... }
46 sub is_json : Chained('start') Consumes('application/json') { ... }
47 sub is_urlencoded : Chained('start') Consumes('application/x-www-form-urlencoded') { ... }
48 sub is_multipart : Chained('start') Consumes('multipart/form-data') { ... }
50 ## Alternatively, for common types...
52 sub is_json : Chained('start') JSON { ... }
53 sub is_urlencoded : Chained('start') URLEncoded { ... }
54 sub is_multipart : Chained('start') FormData { ... }
56 ## Or allow more than one type
60 : Consumes('application/x-www-form-urlencoded')
61 : Consumes('multipart/form-data')
70 This is an action role that lets your L<Catalyst::Action> match on the content
71 type of the incoming request.
73 Generally when there's a PUT or POST request, there's a request content body
74 with a matching MIME content type. Commonly this will be one of the types
75 used with classic HTML forms ('application/x-www-form-urlencoded' for example)
76 but there's nothing stopping you specifying any valid content type.
78 For matching purposes, we match strings but the casing is insensitive.
82 This role requires the following methods in the consuming class.
88 Returns 1 if the action matches the existing request and zero if not.
92 This role defines the following methods
98 Around method modifier that return 1 if the request content type matches one of the
99 allowed content types (see L</http_methods>) and zero otherwise.
101 =head2 allowed_content_types
103 An array of strings that are the allowed content types for matching this action.
107 Boolean. Does the current request match content type with what this actionrole
112 Catalyst Contributors, see Catalyst.pm
116 This library is free software. You can redistribute it and/or modify it under
117 the same terms as Perl itself.