Cataly...Content(3pm)

πŸ“¦
version 5.90132 Β· 6 versions
, in debian trixie, ubuntu resolute, ubuntu stonking

πŸ’– Catalyst::ActionRole::ConsumesContent β€” Match on HTTP Request Content-Type πŸ’–

okay bestie, so this page is Cataly...Content(3pm) from the User Contributed Perl Documentation collection, and honestly? it's giving tiny module, huge slay. it's a Catalyst::ActionRole that lets your action vibe-check the content type of whatever request just walked in. let's get into it πŸŽ€


✨ NAME ✨

Catalyst::ActionRole::ConsumesContent β€” Match on HTTP Request Content-Type

like. that's literally the whole pitch. it matches on the HTTP request's Content-Type. periodt πŸ’…


πŸ¦‹ SYNOPSIS πŸ¦‹

bestie here's the whole flex in code form, copy-paste ready:

    package MyApp::Web::Controller::MyController;

    use base 'Catalyst::Controller';

    sub start : POST Chained('/') CaptureArg(0) { ... }

      sub is_json       : Chained('start') Consumes('application/json') { ... }
      sub is_urlencoded : Chained('start') Consumes('application/x-www-form-urlencoded') { ... }
      sub is_multipart  : Chained('start') Consumes('multipart/form-data') { ... }

      ## Alternatively, for common types...

      sub is_json       : Chained('start') Consume(JSON) { ... }
      sub is_urlencoded : Chained('start') Consumes(UrlEncoded) { ... }
      sub is_multipart  : Chained('start') Consumes(Multipart) { ... }

      ## Or allow more than one type

      sub is_more_than_one
        : Chained('start')
        : Consumes('application/x-www-form-urlencoded')
        : Consumes('multipart/form-data')
      {
        ## ...
      }

      1;
⚠️ ok so heads up, bestie β€” notice how the second block writes Consume(JSON) (no s!) for the first one while the others say Consumes(...). that's exactly how it is in the original, so i'm leaving it alone. copy it verbatim, don't get cute with it πŸ™ˆ

🌸 DESCRIPTION 🌸

so basically? this is an action role that lets your Catalyst::Action match on the content type of the incoming request. main character behavior fr fr πŸ‘‘

ngl, generally when there's a PUT or a POST request, there's a request content body tagging along, and it's got a matching MIME content type. usually that's one of the types you'd see with classic HTML forms β€” like 'application/x-www-form-urlencoded', for example β€” but literally nothing is stopping you from specifying any valid content type. go off ✨

and for matching purposes? we match strings, but the casing is insensitive. so Application/JSON and application/json are the same vibe to us. iykyk πŸ’œ


πŸ’… REQUIRES πŸ’…

this role needs the following methods to exist in the consuming class, no cap:

match

match_captures

these return 1 if the action matches the existing request, and zero if it doesn't. that's it. that's the whole deal 🫢


⭐ METHODS ⭐

this role defines the following methods, bestie, and i'm listing every single one:

match

match_captures

this is an around method modifier, and it returns 1 if the request content type matches one of the allowed content types (peep "http_methods") and zero otherwise. slay βœ…

allowed_content_types

an array of strings β€” these are the allowed content types for matching this action. iconic 🌈

can_consume

Boolean. does the current request's content type match what this actionrole can consume? yes or no, that's the tea πŸ“

list_extra_info

adds the accepted content type to the debug screen. so when you're debugging you can actually see it. obsessed 😭


πŸ‘‘ AUTHORS πŸ‘‘

Catalyst Contributors, see Catalyst.pm. shoutout to the whole group chat, fr fr 🫢


this library is free software. you can redistribute it and/or modify it under the same terms as Perl itself. periodt, legally speaking πŸ’–


and that's a wrap bestie!! this page came to you courtesy of perl v5.40.0 and it's dated 2024-11-15, so go forth, match some content types, and stay iconic βœ¨πŸ¦‹πŸ’…

πŸ“¦ where this page comes from