使用 Web 服务 API 和 Perl 将附件上传到 Bugzilla

发布于 2024-11-30 09:04:19 字数 1731 浏览 1 评论 0原文

我正在尝试使用 Bugzilla Webservices API 自动上传错误附件,但是当我从 Bugzilla 下载它们时,我上传的 Base64 编码消息总是会损坏。

API 文档位于 http://www .bugzilla.org/docs/4.0/en/html/api/Bugzilla/WebService/Bug.html#add_attachment 指定附件需要进行base64编码,所以我使用一段简单的代码读取本地 png 文件,使用 MIME::Base64 并使用名为 BZ::Client 的 Bugzilla Perl 客户端 API 上传。

相关代码如下所示 -

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";        
            };

非常简单。我相信在后端 Web 服务 API 使用decode_base64,所以我很惊讶这不起作用。即使使用 Perl 生成的 base64 字符串直接测试 XMLRPC API 仍然会导致文件损坏。

我也尝试过按照错误报告中的建议删除换行符,但没有效果Bug.add_attachment API 调用的实现。

以前有人有过这样的经历吗?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守护在此方 2024-12-07 09:04:19
read (FILE, $data, -s FILE);

这是错误的:引用自 perldoc -f read

Attempts to read LENGTH characters of data.

关键字是尝试read() 可以自由读取少于 LENGTH 字节的数据。

如果你只是想读取 FILE 的内容,你可以使用:

$data = join("", <FILE>);
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>);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文