wardmw has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I am trying to access a ticket in an RT system with the intention of downloading the attachments for further processing and I am using v0.49 of the rt::client::rest API library.

Unfortunately I seem to have hit a bug in the code. Below is the relevant code piece:

... try { # Try and find some tickets @search = $rt->search(type => 'ticket', query => "id = '3188'" +); } catch RT::Client::REST::UnauthorizedActionException with { print STDERR "You are not authorized to execute this search.\n +"; } catch RT::Client::REST::Exception with { # something went wrong. die "Problem trying to execute a search: ", shift->message; }; exit 0 if (scalar(@search) < 1); foreach $id (@search) { # Retrieve each ticket from RT $ticket = $rt->show(type => 'ticket', id => $id); @attachment_ids = $rt->get_attachment_ids (id => $id); foreach(@attachment_ids) { print "Att_id: $_\n"; $att = $rt->get_attachment (parent_id => $id, id => $_ +); print Dumper($att); print "\n"; } }
Running the above causes the code to print out a few transactions that don't have file names and then crash with:
Use of uninitialized value in split at /usr/lib/perl5/site_perl/5.8.8/ +RT/Client/REST/Forms.pm line 38. Can't use an undefined value as an ARRAY reference at /usr/lib/perl5/s +ite_perl/5.8.8/RT/Client/REST.pm line 167.
The error is caused inside the call to get_attachment(). Does anyone have any example code that does retrieve file attachments or have an explanation to why I get the above error?

|\\/|artin

Replies are listed 'Best First'.
Re: Accessing attachments using RT::Client::REST
by kcott (Archbishop) on Dec 03, 2015 at 08:04 UTC

    G'day wardmw,

    [Disclaimer: I've never used RT::Client::REST and, as I don't have that module installed, I'm unable to provide any local testing for you. What I aim to provide below is some help in troubleshooting your problem.]

    I looked at the source code of (the latest versions of) RT::Client::REST and RT::Client::REST::Forms on CPAN. The line numbers you report don't quite marry up with what I'm seeing, so you're probably using an older version. The paths to your locally installed versions are provided: you should view these while reading the remainder of what I have here. Everything I talk about below refers to the current CPAN versions. You should check that assumptions I'm making about slight changes in line numbers are, in fact, valid for your installed versions.

    Here's the code in &RT::Client::REST::Forms::form_parse (up to, and including, line 36):

    sub form_parse { my $state = 0; my @forms = (); my @lines = split /\n/, $_[0];

    So, the first argument to form_parse() would be the reported uninitialized value.

    Here's the code in &RT::Client::REST::get_attachment (up to, and including, line 175):

    sub get_attachment { my $self = shift; $self->_assert_even(@_); my %opts = @_; my $type = $self->_valid_type(delete($opts{type}) || 'ticket'); my $parent_id = $self->_valid_numeric_object_id(delete($opts{paren +t_id})); my $id = $self->_valid_numeric_object_id(delete($opts{id})); 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]};

    So, $$form[0] would be the undefined value (i.e. not an ARRAY reference).

    Furthermore, the $content, passed to form_parse() is, as previously identified, an uninitialized value.

    It looks like the value of $content is the problem: generating a warning in form_parse() and, subsequently, an error in get_attachment().

    In the code you've posted, you have

    get_attachment (parent_id => $id, id => $_)

    whereas, the documentation for RT::Client::REST has

    get_attachment (parent_id => $parent_id, id => $id, undecoded => $bool +)

    For every element in @attachment_ids, you're assigning the return value of $res->decoded_content to $content which, from your description, works successfully a few times and then fails catastrophically.

    I would concentrate further efforts in this foreach loop. Perhaps you want to skip further processing of some elements (e.g. with an early next) or maybe include undecoded => $bool in the get_attachment() call (setting $bool to either TRUE or FALSE depending on the element being processed).

    — Ken

      Many thanks for your help Ken, it has got me moving again. I added the undecoded option and set it to FALSE which caused exactly the same problems as I had been experiencing without it. Setting it to TRUE however makes everything work.

      This is not quite what I was after since the file I am trying to retrieve is a text file but to be honest I can probable work around any issue that might come up because the file was not decoded and to be honest I have no idea what the undecoded flag actually does other than what the docs say, which isn't much!

      Still, I can move forward with this now so my thanks to you.

      |\\/|artin

Re: Accessing attachments using RT::Client::REST
by dmitri (Priest) on Dec 16, 2015 at 17:00 UTC
    Hi wardmw,

    Author of RT::Client::REST here.

    If you think you've hit a bug, please file a bug report here: rt://RT-Client-REST. The module has several maintainers and one of us is sure to take a look at (and maybe even fix!) the problem.

    Thanks,

       - Dmitri.