Search code examples
perlmime

MIME::Parser fails to parse multipart/mixed parts correctly


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.

Solution

  • The HTTP/1.1 200 OK line is not part of the MIME response. Take it out and it works fine.