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}/attachmentsbut 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..
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Your command looks right. What are the error messages or responses you get back?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.