4.0.5 REST - adding an attachment - php

Heyas,

I’m stuck at how to put an attachment onto a ticket via REST and php.

I’m successfully getting ticket information, creating a ticket, editing
it and adding correspondence to it using 2 methods, curl and
stream_content_create.
Adding an attachment has me baffled. I understand I cannot do it at
ticket creation, need to do it as adding a comment.
I get as far as, Bad Request # No attachment for…

If anyone has an example of how to do it I would be extremely grateful.

Thanks, Alex

Adding an attachment has me baffled. I understand I cannot do it at
ticket creation, need to do it as adding a comment.
I get as far as, Bad Request # No attachment for…

If anyone has an example of how to do it I would be extremely grateful.

http://wiki.bestpractical.com/view/REST#Ticket_History_Comment

Without showing us what you’re trying, we can do nothing but guess. My
guess is that you’re not POSTing the attachment_1 field.

Thanks Thomas

Not using curl
$attachment_1 = file_get_contents(FCPATH . ‘/test.pdf’);

$content = “id: 15818\n”;
$content = $content . “Action: correspond\n”;
$content = $content . “Text: Im adding some text\n And adding some more
text\n And some more\n”;
$content = $content . “TimeWorked: 30\n”;
$content = $content . “Attachment: test.pdf\n”;

$url = ‘http://rt4.challengeltd.net/REST/1.0/’;
$url .= ‘ticket/15818/comment’;

$post_data = array(
‘user’ => ‘user’,
‘pass’ => ‘password’,
‘content’ => $content,
‘attachment_1’ => $attachment_1
);
$data = http_build_query($post_data);
$params = array(
‘http’ => array(
‘method’ => ‘POST’,
‘header’ => ‘Content-type:
application/x-www-form-urlencoded’,
‘content’ => $data
));

$ctx = stream_context_create($params);
$response = file_get_contents($url, false, $ctx);
print_r($response);

Using curl
$attachment_1 = file_get_contents(FCPATH . ‘/test.pdf’);
$content = “id: 15818\n”;
$content = $content . “Action: correspond\n”;
$content = $content . “Text: Im adding some text\n And adding some more
text\n And some more\n”;
$content = $content . “TimeWorked: 30\n”;
$content = $content . “Attachment: test.pdf\n”;

$request = ‘http://rt4.challengeltd.net/REST/1.0/’;
$request .= ‘ticket/15818/comment’;

$postargs = array(
‘user’ => ‘aardalich’,
‘pass’ => ‘BluesPacer’,
‘content’ => $content,
‘attachment_1’ => $attachment_1
);

// get the curl session object
$session = curl_init($request);

// set the POST options
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, $postargs);
curl_setopt($session, CURLOPT_HEADER, true);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $session, CURLOPT_HTTPHEADER, array( ‘Expect:’ ) );

// do the POST and then close the session
$response = curl_exec($session);
print_r($response);
curl_close($session);

Expect I’m not grabbing the raw file correctly, or encoding it or
something. Unsure of how.

I was thinking the other way I could go is (after creating the ticket
and getting the number from the response back) is to then send in an
email with the attachment with the correct subject so it lands in the
ticket.

Thanks, AlexFrom: rt-users-bounces@lists.bestpractical.com
[mailto:rt-users-bounces@lists.bestpractical.com] On Behalf Of Thomas
Sibley
Sent: Thursday, 29 March 2012 12:08 AM
To: rt-users@lists.bestpractical.com
Subject: Re: [rt-users] 4.0.5 REST - adding an attachment - php

Adding an attachment has me baffled. I understand I cannot do it at
ticket creation, need to do it as adding a comment.
I get as far as, Bad Request # No attachment for…

If anyone has an example of how to do it I would be extremely
grateful.

http://wiki.bestpractical.com/view/REST#Ticket_History_Comment

Without showing us what you’re trying, we can do nothing but guess. My
guess is that you’re not POSTing the attachment_1 field.

$params = array(
‘http’ => array(
‘method’ => ‘POST’,
‘header’ => ‘Content-type:
application/x-www-form-urlencoded’,
‘content’ => $data
));

This is certainly the wrong content-type. You want a multipart content
type since uploads generally can’t work url encoded.

$postargs = array(
‘user’ => ‘aardalich’,
‘pass’ => ‘BluesPacer’,
‘content’ => $content,
‘attachment_1’ => $attachment_1
);

You probably want to change that password now…

// get the curl session object
$session = curl_init($request);

// set the POST options
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, $postargs);
curl_setopt($session, CURLOPT_HEADER, true);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $session, CURLOPT_HTTPHEADER, array( ‘Expect:’ ) );

I don’t know enough about the PHP curl interface to tell if the POST is
getting constructed correctly by this code. Since it doesn’t work,
however, I suspect it’s subtly wrong.

Expect I’m not grabbing the raw file correctly, or encoding it or
something. Unsure of how.

The POST data needs to be a multipart MIME entity.

Look at the other language examples of POSTing attachment_1 on the wiki
page I linked to. For debugging, capture the request that’s actually
supposed to look like, capture what /opt/rt4/bin/rt sends when you
submit a comment with an attachment.

Thomas

Will do, thanks for your help.

And yes, as soon as I sent it, argh, forgot one of the user/pass spots
:slight_smile:

Alex