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

0 votes
Accepted answer

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 votes

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

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Wednesday in Jira

Make your Atlassian Cloud products more secure: our NEW admin security guide

Hey admins! I’m Dave, Principal Product Manager here at Atlassian working on our cloud platform and security products. Cloud security is a moving target. As you adopt more products, employees consta...

90 views 0 4
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