perl - 尝试发布时出现“HTTP::消息内容必须为字节”错误

标签 perl post httpwebrequest httprequest libwww-perl

我有以下代码:

...
sub setImage {
    my $self=shift;
    my $filename=shift;
    unless(-r $filename) {
        warn "File $filename not found";
        return;
    }
    my $imgn=shift;
    my $operation=&URI::Escape::uri_escape_utf8(
        (shift) ? "Удалить! (Delete)" : "Сохранить! (Store)");
    my $FH=&::File::open($filename, 0, 0);
    my $image;
    # &utf8::downgrade($image);
    sysread($FH, $image, 102400, 0);
    close $FH;
    my $imginfo=eval{&Image::Info::image_info(\$image)};
    if($@ or $imginfo->{"error"}) {
        warn "Invalid image: ".($@ || $imginfo->{"error"});
        return undef;
    }
    my $fields=[
        DIR       => $self->url("fl"),
        OPERATION => $operation,
        FILE_NAME => ".photo$imgn",
        # FILE      => [$filename],
        FILE      => [undef, "image.".$imginfo->{"file_ext"},
            # Content_Type => $imginfo->{"file_media_type"},
            # Content_Type => 'application/octet-stream',
            Content      => $image,
        ],
    ];
    my $response=&ZLR::UA::post(
        &ZLR::UA::absURL("/cgi-bin/file_manager")."",
        $fields,
        Content_Type => "form-data",
    );
    print $response->decoded_content;
}
...

当我尝试使用函数 setImage 时,它​​失败并出现错误HTTP::Message content must be bytes at/usr/lib64/perl5/vendor_perl/5.8.8/HTTP/Request/Common.pm line 91.更糟糕的是,如果不使用我的所有代码,我就无法重现此错误,并且升级 libwww-perl 不会执行任何操作。是什么原因造成的?

libww-perl 的版本:dev-perl/libwww-perl-5.836。 HTTP::Request 和 HTTP::Request::Common 来自 libwww-perl 包,版本:5.827 和 5.824。

追踪:

HTTP::Message content must be bytes at /usr/lib64/perl5/vendor_perl/5.8.8/HTTP/Request/Common.pm line 91
 at Carp::croak(unknown source)
 at HTTP::Message::__ANON__(/usr/lib64/perl5/vendor_perl/5.8.8/HTTP/Message.pm:16)
 at HTTP::Message::_set_content(/usr/lib64/perl5/vendor_perl/5.8.8/HTTP/Message.pm:136)
 at HTTP::Message::content(/usr/lib64/perl5/vendor_perl/5.8.8/HTTP/Message.pm:125)
 at HTTP::Request::Common::POST(/usr/lib64/perl5/vendor_perl/5.8.8/HTTP/Request/Common.pm:91)
 at LWP::UserAgent::post(/usr/lib64/perl5/vendor_perl/5.8.8/LWP/UserAgent.pm:397)
 at ZLR::UA::post(./zlrchecker.pl:71)
 at ZLR::Info::setImage(./zlrchecker.pl:1754)
 at main::main(./zlrchecker.pl:3893)
 at main::(./zlrchecker.pl:4148)

最佳答案

使用Devel::SimpleTrace并粘贴痕迹。使用 cpan 安装模块。然后使用 -MDevel::SimpleTrace 运行您的程序,例如 perl -MDevel::SimpleTrace ./myapp_run.pl

并粘贴 HTTP::Request:CommonHTTP::MessageLWP 的版本。

我的猜测是您会在堆栈跟踪中看到这一点:

这似乎是the code likely causing the error :

*_utf8_downgrade = defined(&utf8::downgrade) ?
    sub {
        utf8::downgrade($_[0], 1) or
            Carp::croak("HTTP::Message content must be bytes")
    }
    :
    sub {
    };

utf8 中的文档说这个:

Fails if the original UTF-X sequence cannot be represented in the native 8 bit encoding. On failure dies or, if the value of FAIL_OK is true, returns false.

您应该能够通过运行 utf8::downgrade($http_message_content) 来制作测试用例

关于perl - 尝试发布时出现“HTTP::消息内容必须为字节”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2951466/

相关文章:

regex - 用正则表达式匹配一个字符串

perl - 为什么 Perl 的 foreach 不要求用 my 声明它的变量?

regex - Perl 多行正则表达式

c# - 在 C# 中将匿名类型转换为键/值数组?

php - $_POST *not* 中的值可以是字符串吗?

perl - 为什么在生成守护进程时会剥离 utf8 IO 层?

java - 404 访问 https URL 时出错 - Java,但使用 POSTMAN 或 SOAPUI 工作正常

javascript - Jquery $.post() 以 HTML 返回相同的页面

android - 在改造中在 json 对象中发布数组?

c# - 检查URL是否存在-HTTP请求始终返回异常