使用 Web 服务 API 和 Perl 将附件上传到 Bugzilla
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是错误的:引用自
perldoc -f read
关键字是尝试:
read()
可以自由读取少于 LENGTH 字节的数据。如果你只是想读取 FILE 的内容,你可以使用:
This is just wrong: Quote from
perldoc -f read
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: