Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

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

carol yang March 27, 2014

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

1 vote
Dmitry Kochnev
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 14, 2017

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

 

 

1 vote
Nicholas Ng March 12, 2015

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;
Joan Gimenez
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 21, 2017

Worked for me.

0 votes
Michael Kast
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
November 3, 2016

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

0 votes
Michael Bulger March 12, 2015

Was a solution ever found for this?

0 votes
lama saad
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 21, 2014

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

0 votes
Nicholas Ng August 27, 2014

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

Suggest an answer

Log in or Sign up to answer