OK, so I started using the logger object, which provided more information but nothing that I found useful. For the future people reading this the Log::Log4perl module works perfectly for this.

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:

#!/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; } }
Many thanks to you for your patience, help and assistance.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.