Upload/URL Attachment via API on JIRA (PHP)

Is there any way we can attach a file on JIRA Issue using url thru API? 
I mean I am making a two ways of attaching file, either Upload or attach URL.

i am using for the upload

curl -D- -u {username}:{password} -X POST -H "X-Atlassian-Token: nocheck" -F "file=@{path/to/file}" http://{base-url}/rest/api/2/issue/{issue-key}/attachments

but when the user attach a URL this kind of curl is not working. i tried many things buy saving the file in a temp folder of the site and upload it on JIRA but didn't work.. 

2 answers

1 accepted

I solved it my self. Here's my code for reference. I hope it could help others. 
Im using Laravel on this one

* CURL post request to the JIRA REST API for Attachments only
* @param $issueKey - Jira Issue Key
* @param $file - url of the file or Uploaded temp path
* @param $name - name of the file(used on uploaded file) default is null
* @return json Attachment self
private static function postJSONFile( $issueKey, $file, $name = null )

$file_name = $name ? $name : time().'-'.basename($file); //creating file name

$path = public_path( 'temp/'.$file_name ); //creating temp path

file_put_contents( $path , file_get_contents($file)); //saving file in a temp path
$cfile = new \CURLFile( $path ); // initiating CURLFile for preparing the upload
$cfile->setPostFilename( $file_name ); //setting the file name

$data = array('file'=>$cfile); //creating array for Curl Post Fields
$ch = curl_init(); // initiating curl
//setting the headers
$headers = array(
'X-Atlassian-Token: nocheck',
'Content-Type: multipart/form-data'
//setting curl option on array
curl_setopt_array( $ch, array(
CURLOPT_URL => config( 'jira.url' ) . '/rest/api/latest/issue/' . $issueKey . '/attachments',
CURLOPT_USERPWD => config( 'jira.username' ) . ':' . config( 'jira.password' ),
) );
//executing curl
$response = curl_exec( $ch );
curl_close( $ch ); //closing curl
unlink( $path ); //deleting the file
return $response; //returning values

Ok, so what was the problem?

As your original command looked ok, and your code (if I read it correctly) does much the same, what did you actually change to make it work?

looks like he added a header

0 vote

Your command looks right.  What are the error messages or responses you get back?

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,296 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