... problem happens before RT::Client::REST even hands you the data...
Because of this comment I looked a little bit at the source of RT::Client::REST and I noticed that there are two debug/logger lines that could be of interest that allows you to see both the request and the response. These lines are located in the _submit method:
# Then we send the request and parse the response.
$self->logger->debug('request: ', $req->as_string);
my $res = $self->_ua->request($req);
$self->logger->debug('response: ', $res->as_string);
To use it you need to create a custom logger object that needs the methods debug, warn, info, error and pass it in the logger function and write the debug function to print the output somewhere.
I think this may help to pinpoint where the problem is really located (server side, or client side). If the problem is indeed on the server side then I'm afraid there is not much you can do. | [reply] [d/l] |
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.
| [reply] [d/l] [select] |