使用 Web 服务 API 和 Perl 将附件上传到 Bugzilla [英] Uploading attachments to Bugzilla using the Web Services API and Perl

查看:55
本文介绍了使用 Web 服务 API 和 Perl 将附件上传到 Bugzilla的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

相关代码如下-

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 仍然会导致文件损坏.

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.

我也尝试按照 错误报告 中的建议去除换行符,但无济于事a> 关于 Bug.add_attachment API 调用的实现.

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?

谢谢!

推荐答案

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

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

Attempts to read LENGTH characters of data.

关键词是尝试:read()可以自由读取少于 LENGTH 个字节.

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

如果您只想获取 FILE 的内容,可以使用:

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

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

这篇关于使用 Web 服务 API 和 Perl 将附件上传到 Bugzilla的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆