perlencodingdancer

Dancer request params encoding


Let's say that I have a page which is encoded in 'cp1251' and I submit a form then my params will be in 'cp1251'. But when I access my params in Dancer I get only '?'marks. How can I access the data which is passed?


Update:

There seems to be a sub called _decode /bellow/ in Request.pm which is called on every parameter. Is there a way to tell Dancer not to call this sub?

sub _decode {
    my ($h) = @_; 
    return if not defined $h; 

    if (!ref($h) && !utf8::is_utf8($h)) {
        return decode('UTF-8', $h);
    }   

    if (ref($h) eq 'HASH') {
        while (my ($k, $v) = each(%$h)) {
            $h->{$k} = _decode($v);
        }   
        return $h; 
    }   

    if (ref($h) eq 'ARRAY') {
        return [ map { _decode($_) } @$h ];
    }   

    return $h; 
}

Update2:

I found a way to get the data. I had to use request->{_http_body}->{param} but I shouldn't call params before it because it will corrupt it.


Update3:

To make it work I had to remove the charset from the 'config.yaml' and to add

request->{_params_are_decoded} = 1; in a before filter.


Solution

  • The automatic encoding only happens when the "charset" setting is set.

    Disable it in config.yml and you are done.