Get Attachment via script

Hello

first what I would like to achive. When user adds attachment with comment, I need to get it via api/REST.
The attachment can be base64 or just link to where the attachment is saved and I will ecode it in base64 with other script.

I was trying like this but when I add attachment I do not see anything in syslog that would be indication of attachment.

my $attachments = $self->TransactionObj->Attachments;
my $content;
while (my $attach = $attachments->Next) {   
        RT::Logger->error("attach:",  $attach);
        $content = $attach->Content;
        RT::Logger->error("content:",  $content);
}

logs:
May 21 14:44:59 localhost RT: [31652] attach: RT::Attachment=HASH(0x55df6547e988)
May 21 14:44:59 localhost RT: [31652] CustumeFileds: ÿØÿà

thank you!
miha

That appears to match your first RT::Logger call, so the $attach does appear to be a Perl hash for the RT::Attachment object. However:

does not match the next RT::Logger output, which seems a bit odd - it should at least say “content:” even if the $content variable is empty.

Are you sure you don’t have another RT::Logger that would output these lines elsewhere? And you are sure you’ve cleared the Mason cache and restarted the webserver so that the running code matches what you’ve shown us (rather than a cached older version)?

Hi,

yes I restarted apache2 and RT, cleared mason cache (obj directory) and restart everything again just to be sure.

My whole log like this:

May 23 11:00:52 localhost RT: [6095] First before while: RT::Attachments=HASH(0x5602f936c490)
May 23 11:00:52 localhost RT: [6095] Use of uninitialized value $_[1] in join or string at /usr/local/share/perl/5.24.1/Log/Dispatch.pm line 25.
May 23 11:00:52 localhost RT: [6095] attach:
May 23 11:00:52 localhost RT: [6095] attachment in while: RT::Attachment=HASH(0x5602f933ccb0)
May 23 11:00:52 localhost RT: [6095] Use of uninitialized value $_[1] in join or string at /usr/local/share/perl/5.24.1/Log/Dispatch.pm line 25.
May 23 11:00:52 localhost RT: [6095] content in while:
May 23 11:00:52 localhost RT: [6095] Use of uninitialized value $_[1] in join or string at /usr/local/share/perl/5.24.1/Log/Dispatch.pm line 25.
May 23 11:00:52 localhost RT: [6095] attach:
May 23 11:00:52 localhost RT: [6095] attachment in while: RT::Attachment=HASH(0x5602f94eefe8)
May 23 11:00:52 localhost RT: [6095] content in while: <div>On Thu May 09 10:21:06 2019, root wrote:#012<blockquote>This transaction appears to have no content</blockquote>#012</div>
May 23 11:00:52 localhost RT: [6095] attach: <div>On Thu May 09 10:21:06 2019, root wrote:#012<blockquote>This transaction appears to have no content</blockquote>#012</div>
May 23 11:00:52 localhost RT: [6095] attachment in while: RT::Attachment=HASH(0x5602f8e2f438)
May 23 11:00:52 localhost RT: [6095] content in while: ÿØÿà

In my whole code:

use strict;
use warnings;
use LWP::UserAgent;
use Data::Dumper;
 
use Encode qw(encode_utf8);
use HTTP::Request();
use JSON::MaybeXS qw(encode_json);


my $ua = LWP::UserAgent->new;


my $url = 'http://172.31.1.121:8888/test';
my $header = ['Content-Type' => 'application/json; charset=UTF-8'];
my $data = {status => $self->TicketObj->Status};
my $encoded_data = encode_utf8(encode_json($data));
 
my $r = HTTP::Request->new('POST', $url, $header, $encoded_data);

RT::Logger->error("First before while:",  $self->TicketObj->Attachments);

my $attachments = $self->TransactionObj->Attachments;
my $content;

while (my $attach = $attachments->Next) {   
RT::Logger->error("attach:",  $content);
        RT::Logger->error("attachment in while:",  $attach);
        $content = $attach->Content;
        RT::Logger->error("content in while:",  $content);
}

return 1;

What I do, I open ticket and add attachement in comment. If I am doing something wrong…

The line RT::Logger->error("attach:", $content); is confusing things slightly, because the $content variable is declared outside the loop and is initially undefined, and then on subsequent passes is showing the value of $content from the previous pass. Its adding noise to your debugging, so I’d ditch it.

It looks to me that this ticket has three attachments:

  • One has no content (which is a valid thing to happen),
  • One has the content <div>On Thu May 09 10:21:06 2019, root wrote:#012<blockquote>This transaction appears to have no content</blockquote>#012</div>,
  • One has the content ÿØÿà.

What did you actually attach to the ticket that you’re expecting to see?

Hi, first thank you for help!

I am attaching rght now for test .png, .pdf files. What I would like to see is some link or base64 of attachment so that I am later able to send it with REST.

thx!

So you do see those attachments attached to the ticket in the normal RT web ticket display, right?

You might want to include a new logging line to output the content type associated with each attachment:

RT::Logger->error(“attachment type:”, $attach->ContentType());

Also I’d put braces after the call the Content method:

$content = $attach->Content();

Thank you.

May 23 14:27:19 localhost RT: [6091] attachment type: text/html
May 23 14:27:19 localhost RT: [6091] attachment in while: RT::Attachment=HASH(0x5602f9830d78)
May 23 14:27:19 localhost RT: [6091] content in while: <div>On Thu May 23 12:42:03 2019, root wrote:#012<blockquote>#012<div>On Wed Apr 24 09:51:20 2019, root wrote:#012<blockquote>#012<pre>#012asd</pre>#012</blockquote>#012</div>#012</blockquote>#012</div>

How would I get now attachment :)?

tnx!

Umm… you’ve got the attachment. That’s what’s in the content (text/html which is what that appears to be).

eh, sorry I was to quick.

Yes, text is ok, but attachment which was attached via www is not logged. So first it is .txt attachmetn and “content in while” is empty, second is written text in ticket which is ok.

May 23 14:36:06 localhost RT: [6042] attachment type: multipart/mixed
May 23 14:36:06 localhost RT: [6042] Use of uninitialized value $_[1] in join or string at /usr/local/share/perl/5.24.1/Log/Dispatch.pm line 25.
May 23 14:36:06 localhost RT: [6042] content in while:
May 23 14:36:06 localhost RT: [6042] Use of uninitialized value $_[1] in join or string at /usr/local/share/perl/5.24.1/Log/Dispatch.pm line 25.
May 23 14:36:06 localhost RT: [6042] attach:
May 23 14:36:06 localhost RT: [6042] attachment type: text/html
May 23 14:36:06 localhost RT: [6042] content in while: <div>asdasdasdasdaddsad<br />#012<br />#012On Wed Apr 24 09:51:20 2019, root wrote:#012<blockquote>#012<pre>#012asd</pre>#012</blockquote>#012</div>

Hello

I see that also in DB under colume “Content”, when you attache file there is no data but the file is inserted in RT under ticket.

I guess I must get this data in some other way. If this is typed ticket (text/plain), than it is ok.

The multipart/alternative could be the container that holds the other test/plain and text/html attachments (ie those two are alternative representations of the same content).

@GreenJimll ok :slight_smile: how can i get this attachments via api :)? as in case of attachment “$attach->Content()” this is empty.

tnx!

If its a multipart/alternative MIME type, what it contains are other, alternative MIME attachment parts, which I assume are the two attachments following it (the text/plain and text/html). Those two attachments will have the first attachment marked as their “Parent” in the database, linking them to it.

You can check if an attachment has child attachments using the RT::Attachment->Children() method. There’s also RT::Attachment->Siblings() if you have one of the child attachments and want to find the other attachments with the same parent attachment, and RT::Attachment->ParentObj() if you want the parent attachment for an attachment (which will be undef if the attachment you’re looking at is a top level one).

What I’m not seeing in your database extract is any mention of attachments with MIME content types of image/png or application/pdf. Have you checked that you can see those attachments via the normal RT web user interface for the ticket?

Yes, this i have checked and I can see tham, also I am able to download them. So this should be also in DB in table Attachments?

OK, if you can see them in the web user interface, take a look at the URL you have for the attachment. It will look something like:

https://yourserver.example.com/Ticket/Attachment/967392/878482/image003.png

The first number (967392) is the transaction Id and the second is the attachment Id (878482). You could then use those to compare with the transaction Id and attachment Id you’re getting from the Perl API by printing those out in your loop:

RT::Logger->error(“attachment id”, $attach->id);
RT::Logger->error(“attachment transaction Id:”, $attach->TransactionObj->id);
RT::Logger->error(“attachment ticket Id:”, $attach->TransactionObj->ObjectId);

@GreenJimll thank you, now it is ok.

Just one final question. Now I get all attachments for comment. So if I comment on comment which has already attachment I will not get only the new attachment that I added but also the attachment which was already in that comment.

I would like to get only attachment that has beed added now (with comment or replay)

thank you!