Robert Conn
Robert Conn

Reputation: 877

Uploading attachments to Bugzilla using the Web Services API and Perl

I'm experimenting with the Bugzilla Webservices API for uploading attachments to bugs automatically but the base64 encoded messages I'm uploading always end up corrupted when I download them from Bugzilla.

The API doc at http://www.bugzilla.org/docs/4.0/en/html/api/Bugzilla/WebService/Bug.html#add_attachment specifies that the attachment needs to be base64 encoded, so I'm using a straightforward piece of code to read a local png file, convert to base64 using MIME::Base64 and uploading using a Bugzilla Perl client API called BZ::Client.

The relevant code looks like this -

my $client = BZ::Client->new("url" => $url,
                             "user" => $user,
                             "password" => $password);

            open (FILE, "$file") or die "$!";
            binmode FILE;
            read (FILE, $data, -s FILE);
            $base64_encoded_file = encode_base64($data);

            my %params = (
                ids => [ 1 ],
                data => $base64_encoded_file,
                file_name => 'filename.png',
                content_type => "image/png",
                summary => 'blah blah' );


            my $response = '';

            eval {
                $response = $client->api_call("Bug.add_attachment", \%params); # Needs to be hash ref
            } or do {
                print "ERROR: $@\n";        
            };

So fairly straightforward. I believe on the backend the Web Service API uses decode_base64 so I'm surprised this doesn't work. Even a direct test of the XMLRPC API with the generated base64 string from the Perl still results in a corrupt file.

I have also tried stripping line breaks to no avail as suggested in the bug report about the implementation of the Bug.add_attachment API call.

Anyone had any experience of this before?

Thanks!

Upvotes: 4

Views: 1786

Answers (1)

adrian
adrian

Reputation: 11

read (FILE, $data, -s FILE);

This is just wrong: Quote from perldoc -f read

Attempts to read LENGTH characters of data.

The key word is Attempts: read() is free to read LESS than LENGTH bytes.

If you just want to slurp the contents of FILE you could use:

$data = join("", <FILE>);

Upvotes: 1

Related Questions