[Catalyst] Help with weird client: POST without Content-Type

Andrew Rodland andrew at cleverdomain.org
Fri Dec 18 11:33:32 GMT 2009


On Friday 18 December 2009 05:11:56 am Alex Povolotsky wrote:
> Hello!
> 
> I'm (still) working with weird client-weird server-weird DB system, and
> I need some more help from community.
> 
> Weird self-written client sends POST request without setting
> Content-Type at all.
> 
> Client will be fixed, but until then I have to force Catalyst to parse
> that request as if it's type was application/x-www-form-urlencoded.
> 
> How do I do that?
> 
Here's a suggestion -- not sure if it's the best one, but it should work. In 
your app class (MyApp.pm or whatever) do:

before 'prepare_body' => sub {
    my $c = shift;
    if ($c->req->method eq 'POST' && !defined $c->req->content_type) {
        $c->req->content_type('application/x-www-form-urlencoded');
    }
};

The actual decision on how to parse the body is done by HTTP::Body, not by 
Catalyst, so it can't easily be overridden, but using this trick we hook into 
Catalyst after the headers are parsed but before HTTP::Body is invoked, and 
fool it into thinking that the Content-Type actually is what you want it to 
be.

Andrew



More information about the Catalyst mailing list