代码之家  ›  专栏  ›  技术社区  ›  ZyX

尝试发布时发生«http::message content must be bytes»错误

  •  2
  • ZyX  · 技术社区  · 14 年前

    我有以下代码:

    ...
    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)
    
    2 回复  |  直到 10 年前
        1
  •  4
  •   Evan Carroll    14 年前

    使用 Devel::SimpleTrace 并粘贴痕迹。用CPAN安装模块。然后用 -MDevel::SimpleTrace 喜欢 perl -MDevel::SimpleTrace ./myapp_run.pl

    粘贴的版本 HTTP::Request:Common , HTTP::Message LWP .

    我猜你会在堆栈跟踪中看到:

    这似乎是 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 这样说:

    如果原始UTF-X序列 不能在本机8中表示 位编码。失败时死亡,或者,如果 fail_ok的值为真,返回 错误的。

    您应该能够通过运行 utf8::downgrade($http_message_content)

        2
  •  0
  •   f055    10 年前

    对于这个问题,我发现的一个有效的解决方案是,在HTTP请求期间,通过 unidecode() Text::Unidecode .

    推荐文章