Jira attach file to issue with PHP and CURL

I have found several examples on how to upload an attachment to an issue in jira, however I have not been able to make any of them work. I posted this question on the Jira Community Help Forums but it has been over a week with 0 replies so hoping the community here can help.

Here is my current attempt:

 $Jirausername = 'myUsername';    $Jirapassword = 'myPassword';    $ch=curl_init();$headers = array(
    'X-Atlassian-Token: nocheck',
    'Content-Type: multipart/form-data'
);$data = array('file' => "testing.txt");
curl_setopt_array(    $ch,    array(        CURLOPT_URL=>'https://myCompany.net/rest/api/latest/issue/TAG-78/attachments',        CURLOPT_POST=>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=>"$Jirausername:$Jirapassword"
    )
);$result=curl_exec($ch);$ch_error = curl_error($ch);
if ($ch_error) {    echo "cURL Error: $ch_error";
} else {    var_dump($result);
}curl_close($ch);

testing.txt is in the same directory as this file. I have curl installed on the webserver where this is hosted, can create issues in jira fine, just cant seem to upload attahcments...

When I run this page it displays:

string(0) "" 

No attachment is uploaded as well needless to say. Any ideas what I am doing wrong?

EDIT: here are some things I have tried:

  1. trying both nocheck and no-check
  2. trying both @testing.txt and testing.txt
  3. removing 'Content-Type: multipart/form-data'
  4. Full paths as such: $data = array('file'=>"@C:\xampp\htdocs\Website\testing.txt ,'filename'=>'testing.txt');
  5. Tried like this too because of a known curl error: $data = array('file'=>"@C:\xampp\htdocs\Website\testing.txt" ';filename=testing.txt');

And every combination of those above. No matter what I try it does not work though. Have also ensured I am an admin level user in Jira. I feel like my code should work... but clearly not.

1 answer

0 vote
Rian Spivak Community Champion Nov 03, 2017

Looks like you haven't loaded the file's data into the postbody to send with the web request. Here's a link to a PHP file for a REST request using curl that handles a file attachment. Look at the buildpostbody function.

Part of the attachment process is loading the file content and adding it to the postbody as a string. It also provides the correct Content identifier as well based on the file contents. Just providing the filename to the curl function isn't sufficient.

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,323 views 14 20
Join discussion

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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot