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

This widget could not be displayed.

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' ),
CURLOPT_POST => 1,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_VERBOSE => 1,
CURLOPT_POSTFIELDS => $data,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers,
) );
//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

This widget could not be displayed.

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

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

269 views 5 0
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