JIRA - How to add attachment for issues using REST API (PHP)

Hi

I'm now working on attaching attachment for issues based on REST API using PHP. This is my code:

<?php

$ch=curl_init();

$headers = array(

'X-Atlassian-Token: nocheck',

'Content-Type: multipart/form-data'

);

$data = array("file" => "attachments/test.txt");

curl_setopt_array(

$ch,

array(

CURLOPT_URL=>'http://localhost:8080/rest/api/latest/issue/IS-23/attachments',

CURLOPT_POST=>true,

//CURLOPT_HTTPGET =>true,

CURLOPT_VERBOSE=>1,

CURLOPT_POSTFIELDS=>$data,

CURLOPT_SSL_VERIFYHOST=> 0,

CURLOPT_SSL_VERIFYPEER=> 0,

CURLOPT_RETURNTRANSFER=>true,

CURLOPT_HEADER=>false,

CURLOPT_HTTPHEADER=> $headers,

CURLOPT_USERPWD=>"test:test"

)

);

$result=curl_exec($ch);

$ch_error = curl_error($ch);

if ($ch_error) {

echo "cURL Error: $ch_error";

} else {

var_dump($result);

}

curl_close($ch);

?>

=============

However, I could got any result. Any ideas?

Thanks

6 answers

This widget could not be displayed.

The following code works for me (tested on JIRA version 6.3.12):

&lt;?php
$ch=curl_init();
$headers = array(
    'X-Atlassian-Token: nocheck',
    'Content-Type: multipart/form-data'
);
$data = array("file" =&gt; "@filepath/filename.png;filename=filename.png");
curl_setopt_array(
    $ch,
    array(
        CURLOPT_URL=&gt;'http://localhost:8080/rest/api/latest/issue/TEST-01/attachments',
        CURLOPT_POST=&gt;true,
        CURLOPT_VERBOSE=&gt;1,
        CURLOPT_POSTFIELDS=&gt;$data,
        CURLOPT_SSL_VERIFYHOST=&gt; 0,
        CURLOPT_SSL_VERIFYPEER=&gt; 0,
        CURLOPT_RETURNTRANSFER=&gt;true,
        CURLOPT_HEADER=&gt;false,
        CURLOPT_HTTPHEADER=&gt; $headers,
        CURLOPT_USERPWD=&gt;"userid:password"
    )
);
$result=curl_exec($ch);
$ch_error = curl_error($ch);
if ($ch_error) {
    echo "cURL Error: $ch_error";
} else {
    var_dump($result);
}
curl_close($ch);
?&gt;
This widget could not be displayed.

Hi All,

 

Just confirmed that it is working well on cURL (PHP 5.6)

public function upd_to($resource, $data, $issueID, $jiraURL, $jiraUserName, $jiraUserPassword)
{
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_POST => 1,
CURLOPT_URL => $jiraURL . '/rest/api/latest/' . $resource . '/' . $issueID . '/attachments',
CURLOPT_USERPWD => $jiraUserName . ':' . $jiraUserPassword,
CURLOPT_POSTFIELDS => $data,
CURLOPT_VERBOSE => 0,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('X-Atlassian-Token: nocheck')
));
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result, true);
}

 

Please pay attention to:

$data = array(
'file' => curl_file_create($filePath)
);

 

For PHP 5.5+ we should use "curl_file_create" function instead of:

$upd_issue = array(
'file' => "@" . $filePath . ";filename=" . "TEST.mp3"
);

 

 

This widget could not be displayed.

I have the same problem adding attachment via PHP REST API. Anybody have any working code example or point to some refernece?

This widget could not be displayed.

Hello,you should add  '@' character before the file example :@file.txt and you will have your file attached @carol yang @Nicholas Ng

This widget could not be displayed.

Was a solution ever found for this?

This widget could not be displayed.

Hello, was anybody of you able to solve the problem?

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted yesterday in Canada

AUG Lunch Roundtable - August 13 - Highlights

Thanks to everyone who came out today for the lunch AUG - we had some great food and discussion (and ice cream sandwiches to fend off the heat!) The welcome and fun fact was great as well, what an aw...

30 views 1 3
View post

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you