I went back over my code and switched from using the ticket->attachments pointer (which doesn't reference the undecoded option as far as I can tell) to the get_attachment_ids / get_attachment loop way. I also created a one line text file, zipped it up and added it to a new RT ticket on my system so that I had a 75 byte gzip file to test with. This worked, I was able to download, unzip and read the file, so I went back and tried the original test ticket, which also worked. I will post the working code below.
Having got this to work I went back over my old code to see what happened before and the answer is, as always, a simple one. Instead of using the undecoded => 1 parameter I had uudecoded => 1. This seemed reasonable to my eye as I scoured the code for errors because I Know Stuff (tm), I knew that uuen/decoding was a valid way of coding characters so didn't question it, it looked right.
So the fault was all mine, a simple typo, although I will add an RFE to the RT::Client::REST developers to add warnings if unknown options are specified as this would have saved me and you a few weeks worth of debugging. The final, working code looks like this:
Many thanks to you for your patience, help and assistance.#!/usr/bin/env perl # # wctest.pl - A test to retrieve and save an attachment. use strict; use warnings; use RT::Client::REST; use RT::Client::REST::Ticket; use Log::Log4perl; my $user='xxxx'; my $pass='yyyy'; my $rt = RT::Client::REST->new( server => ('https://rt.local'), basic_auth_cb => ( sub { return ($user, $pass); } ), ); $rt->login( username=> $user, password=> $pass,); # # Get attachments using get_attachment # my @results = $rt->search( type => 'ticket', query => "id=51447" ); my ($id, @atch_ids, $atch_id, $atch); for $id (@results) { @atch_ids = $rt->get_attachment_ids( id => $id); for $atch_id (@atch_ids) { $atch = $rt->get_attachment (parent_id => $id, id => $atch_id, + undecoded => 1); next if (! defined($atch->{'Filename'}) ); next if ($atch->{'Filename'} eq ''); open(FH, ">", $atch->{'Filename'}) || die "Can't open file: $! +"; syswrite(FH, $atch->{'Content'}); close FH; } }
In reply to Re^3: RT::Client turns occasional binary characters in to wide characters
by wardmw
in thread RT::Client turns occasional binary characters in to wide characters
by wardmw
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |