Dancer 请求参数编码
假设我有一个用“cp1251”编码的页面,并且我提交了一个表单,那么我的参数将在“cp1251”中。但是当我在 Dancer 中访问我的参数时,我只得到“?”标记。我如何访问传递的数据?
更新:
Request.pm 中似乎有一个名为 _decode /bellow/ 的子程序,每个参数都会调用它。有没有办法告诉 Dancer 不要调用这个 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;
}
更新2:
我找到了一种获取数据的方法。 我必须使用 request->{_http_body}->{param}
但我不应该在它之前调用 params
因为它会损坏它。
更新3:
为了使其工作,我必须从“config.yaml”中删除charset
并添加
request->{_params_are_decoded} = 1;< /code> 在前置过滤器中。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅当设置“字符集”设置时才会发生自动编码。
在 config.yml 中禁用它就完成了。
The automatic encoding only happens when the "charset" setting is set.
Disable it in config.yml and you are done.