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

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;

Worked for me.

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"
);

 

 

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

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

Was a solution ever found for this?

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

Suggest an answer

Log in or Sign up to answer
Community showcase
Published yesterday in Agile

How Scrum works? It starts with training and education

To answer “How scrum works,” most of the teams I've worked with first addressed the question: “where to start?”  That question applies to both implementation and improvements on the Scrum framew...

165 views 3 5
Read article

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