Forums

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

Upload/URL Attachment via API on JIRA (PHP)

JeromeFlores November 8, 2016

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

3 votes
Answer accepted
JeromeFlores November 9, 2016

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
}
Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 9, 2016

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?

Bill Gray
Contributor
May 31, 2017

looks like he added a header

0 votes
Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 8, 2016

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

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, likes for trees, atlassian community, social impact, tree planting campaign, community kudos, atlassian giving, environmental impact, sustainability, likes for good, atlassian social responsibility, community challenge

Make every click count—help us plant 50,000 trees! 🌳

Want to make your everyday Community actions directly contribute to reforestation? The Atlassian Community can achieve this goal by liking a post, attending an ACE, sending your peers kudos, and so much more!

Help us plant more trees
AUG Leaders

Atlassian Community Events