MIME::Parser 无法正确解析多部分/混合部分
我有一个服务器向我发送此响应。但是,使用以下代码,我最终得到一个不包含任何部分且边界(包含)之间的所有内容的实体作为 MIME::Body。除了实现我自己的多部分解析器(通常可能有更多部分)并废弃应该为我做这件事的模块之外,我还能做些什么吗?
#!/usr/bin/perl
use MIME::Parser;
my $response = <<_EOF;
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Content-Type: multipart/mixed; boundary="be4dc417ebd640944ab26f033e5ea1ab"
--be4dc417ebd640944ab26f033e5ea1ab
Content-Type: application/json
{"a":"b"}
--be4dc417ebd640944ab26f033e5ea1ab--
_EOF
my $mime_parser = new MIME::Parser;
$mime_parser->tmp_to_core(1);
$mime_parser->output_to_core(1);
my $entity = $mime_parser->parse_data($response);
print "$MIME::Parser::VERSION $^V $^O\n\n";
$entity->print(\*STDOUT);
print "\n\n";
print $entity->parts(0)->bodyhandle->as_string;
输出:
5.502 v5.10.1 MSWin32
--be4dc417ebd640944ab26f033e5ea1ab
Content-Type: application/json
{"a":"b"}
--be4dc417ebd640944ab26f033e5ea1ab--
Can't call method "bodyhandle" on an undefined value at test.pl line 25.
I have a server sending me this response. However, using the following code I end up with an entity containing no parts and everything between the boundaries (inclusive) as the MIME::Body. Is there something I can do short of implementing my own multipart parser (in general there may be more parts) and scrapping the modules that are supposed to do it for me?
#!/usr/bin/perl
use MIME::Parser;
my $response = <<_EOF;
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Content-Type: multipart/mixed; boundary="be4dc417ebd640944ab26f033e5ea1ab"
--be4dc417ebd640944ab26f033e5ea1ab
Content-Type: application/json
{"a":"b"}
--be4dc417ebd640944ab26f033e5ea1ab--
_EOF
my $mime_parser = new MIME::Parser;
$mime_parser->tmp_to_core(1);
$mime_parser->output_to_core(1);
my $entity = $mime_parser->parse_data($response);
print "$MIME::Parser::VERSION $^V $^O\n\n";
$entity->print(\*STDOUT);
print "\n\n";
print $entity->parts(0)->bodyhandle->as_string;
Output:
5.502 v5.10.1 MSWin32
--be4dc417ebd640944ab26f033e5ea1ab
Content-Type: application/json
{"a":"b"}
--be4dc417ebd640944ab26f033e5ea1ab--
Can't call method "bodyhandle" on an undefined value at test.pl line 25.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
HTTP/1.1 200 OK
行不是 MIME 响应的一部分。拿出来就可以正常使用了。The
HTTP/1.1 200 OK
line is not part of the MIME response. Take it out and it works fine.