Uploading attachments to Bugzilla using the Web Services API and Perl

风流意气都作罢 提交于 2019-12-10 17:37:01

问题


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!


回答1:


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>);


来源:https://stackoverflow.com/questions/7131197/uploading-attachments-to-bugzilla-using-the-web-services-api-and-perl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!