Subject: | Attachment retrieval returns wrongly decoded files |
Date: | Thu, 07 Nov 2013 11:25:54 +0100 |
To: | bug-RT-Client-REST [...] rt.cpan.org |
From: | Marco Pessotto <melmothx [...] gmail.com> |
As far as I can see, when retrieving binary attachments, the response of
RT is a text/plain response in any case, with a declared content type:
Content-Type: text/plain; charset=utf-8
The response looks like so:
RT/4.0.7 200 Ok
id: 873
Subject:
Creator: 12
Created: 2013-11-06 07:15:36
Transaction: 1457
Parent: 871
MessageId:
Filename: prova2.png
ContentType: image/png
ContentEncoding: base64
Headers: Content-Type: image/png; name="prova2.png"
Content-Disposition: attachment; filename="prova2.png"
Content-Transfer-Encoding: base64
Content-Length: 16175
Content: <here starts the binary data>
Now, the get_attachment method will call ->decoded_content on the
response, and from what I can see, the binary data will be mangled, as
will be considered utf8 data and decoded accordingly. From my testing,
encoding it back doesn't restore the original binary data.
Please consider the following patch, which adds an option to retrieve
the undecoded data and keeps the existing behaviour unmodified.
diff --git a/lib/RT/Client/REST.pm b/lib/RT/Client/REST.pm
index 8ebef0f..ebbd669 100644
--- a/lib/RT/Client/REST.pm
+++ b/lib/RT/Client/REST.pm
@@ -154,9 +154,16 @@ sub get_attachment {
my $parent_id = $self->_valid_numeric_object_id(delete($opts{parent_id}));
my $id = $self->_valid_numeric_object_id(delete($opts{id}));
- my $form = form_parse(
- $self->_submit("$type/$parent_id/attachments/$id")->decoded_content
- );
+ my $res = $self->_submit("$type/$parent_id/attachments/$id");
+ my $content;
+ if ($opts{undecoded}) {
+ $content = $res->content;
+ }
+ else {
+ $content = $res->decoded_content;
+ }
+ my $form = form_parse($content);
+
my ($c, $o, $k, $e) = @{$$form[0]};
if (!@$o && $c) {
@@ -951,12 +958,16 @@ B<bcc>, and B<attachments> parameters (see C<comment> above).
Get a list of numeric attachment IDs associated with ticket C<$id>.
-=item get_attachment (parent_id => $parent_id, id => $id)
+=item get_attachment (parent_id => $parent_id, id => $id, undecoded => $bool)
Returns reference to a hash with key-value pair describing attachment
C<$id> of ticket C<$parent_id>. (parent_id because -- who knows? --
maybe attachments won't be just for tickets anymore in the future).
+If the option undecoded is set to a true value, the attachment will be
+returned verbatim and undecoded (this is probably what you want with
+images and binary data).
+
=item get_transaction_ids (parent_id => $id, %opts)
Get a list of numeric IDs associated with parent ID C<$id>. C<%opts>
Best wishes
--
Marco