Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Create Issue with attachment?

Hi.

Its no problem to create new issues using the rest api and curl.

But now i would like to attach an image file. Is that possible with "rest/api/2/issue/" or do i have to use the "/rest/api/2/attachment/" statement?

atm my array looks like that:

'fields' => array(
                'project' => array(
                    'key' => $key,
                ),
                'summary' => $summaryText,
                'description' => $text,
                "issuetype" => array(
                    "self" => "xxxx",
                    "id" => "3",
                    "description" => "xxxx",
                    "iconUrl" => "xxxx",
                    "name" => "xxx",
                    "subtask" => false
                ),
                'attachment' => array(
                    'content' =>'xxx'
                ),
            ),
        );

there i get this error:

{"errorMessages":[],"errors":{"attachment":"Field does not support update 'attachment'"}}

so the attachment field is not supported or what does this error mean?


12 answers

1 vote

You would have to create an issue, grab the issuekey then attach the attachment using /rest/api/2/issue/issuekey/attachments

mhm ok thx.

but how has to be my postfile?

i always get aHTTP Status 415 -Error

description The server refused this request because the request entity is in a format not supported by the requested resource for the requested method ().

so i build a wrong array?

has someone the syntax for me pls?

i tried:

$data = array("file" => "@fileURL");

 $data = array(
         'attachment' => array(
             "filename" => 'test',
             "content" => 'fileURL'
         ),

 );

 $data = array(
     'fields' => array(
         'attachment' => array(
             "filename" => 'test',
             "content" => 'fileURL'
         ),
     ),
 );

How do you send the POST method? What is the content type you've specified?

nobody knows :( ?

my code should work. I can create issues and add comments with it. but now i dont get it how to attach attachments :/

my php code:

<?php

$username = 'xxx';
$password = 'xxx';
$url = 'xxxx/jira/rest/api/2/issue/' . $issuekey . '/attachments';

$data = "array for attachment";

$ch = curl_init();

$headers = array(
    'Accept: application/json',
    'Content-Type: application/json'
);


curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_VERBOSE, 1);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");



$result = curl_exec($ch);

$ch_error = curl_error($ch);



if ($ch_error) {

    echo "cURL Error: $ch_error";
} else {

    echo $result;
}



curl_close($ch);
?>

i dont get it...

i always get "HTTP Status 415 Error". So something have to be wrong with my array?

Sometimes i got "cURL Error: failed creating formpost data" too.

when someone already attached files using jira rest api and curl, pleasy let me see your code. Its drivin me crazy...

I am grateful for any advice

chris

oki it works now ;)

one question left.

Is it possible to upload files from a different folder or have they be in the same folder as the script is?

now it is:

$data = array(
"file" => $filename);

this works fine, as long my file is in the same folder.

but i would like to get the files from a different folder.

for example something like this:

$data = array(
"file" => $path . $filename);

This attaches a file in jira too. But the created attachment looks then like this:

"self":"https://office.siwa.at/jira/rest/api/2/attachment/11337",
    "id":"11337",
    "filename":"/var/www/html/siwa/chuber/uploads/tx_kundenverwaltungfinal/index.jpg",
    "size":7593,
    "mimeType":"image/jpeg",
    "content":"https://office.siwa.at/jira/secure/attachment/11337/%2Fvar%2Fwww%2Fhtml%2Fsiwa%2Fchuber%2Fuploads%2Ftx_kundenverwaltungfinal%2Findex.jpg",
    "thumbnail":"https://office.siwa.at/jira/secure/thumbnail/11337/_thumb_11337.png"

and thats not the result i would like to get.

I would like smth like this:

    "self":"https://office.siwa.at/jira/rest/api/2/attachment/11337",
    "id":"11337",
    "filename":"index.jpg",
    "size":7593,
    "mimeType":"image/jpeg",
    "content":"https://office.siwa.at/jira/secure/attachment/11337/index.jpg",
    "thumbnail":"https://office.siwa.at/jira/secure/thumbnail/11337/_thumb_11334.png
}



Would have been good if you could have posted the working code..

Like # people like this

Christoph Huber let me now how to attache file to issue

Like Frederik Hintz likes this

Hi Christoph Huber;

Were you able to post an attachment when creating an issue?

I am facing the same issue, I am able to create an issue via POST but without attachment.

May you help me please?

Try using REST API Browser implemented in your JIRA: http://yourJiraServer/plugins/servlet/restbrowser

It will make your life easier if you need to debug your REST calls :)

Hi Chistopher did you find the answer to your last problem. I have run into same one. If you have please help me.

Maybe 'm wrong but I think it doesn't work unless you use CURL command. I have tried from .NET application and from node.js without success.

However using CURL from the command line it works fine. Here is the sample posted on Atlassian documentation

curl -D- -u admin:admin -X POST -H "X-Atlassian-Token: nocheck" -F "file=@myfile.txt" http://myhost/rest/api/2/issue/TEST-123/attachments

This approach works for me.. but the file name in the jira shows as the full path..

<?php

	$username = 'xxx';
	$password = 'xxx';

	$url = 'http://xxx/rest/api/latest/issue/TEST-1/attachments';
	
	$data = array("file" => "@C:/Users/xxx/Desktop/xxxn.png");;
	
	$headers = array(
    //'Accept: application/json',
    //'Content-Type: application/json',
	'X-Atlassian-Token: nocheck'
	);

	$curl = curl_init();
	curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
	curl_setopt($curl, CURLOPT_URL, $url);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
	curl_setopt($curl, CURLOPT_VERBOSE, 1);
	curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
	curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

	$result = curl_exec($curl);
 
	$ch_error = curl_error($curl);
	 
	 
	 
	if ($ch_error) {
	 
		echo "cURL Error: $ch_error";
	} else {
	 
		echo $result;
	}
	
	curl_close($curl);
	
?>

Hi Mohamad,

I have updated your code a bit to rename the file. This works for me on PHP version 5.4.12

<?php
    $username = 'xxx';
    $password = 'xxx';
 
    $url = 'http://xxx/rest/api/latest/issue/TEST-1/attachments';
    $fileLocation = 'Full/path/to/file';
    $fileName = 'desired_filename'
     
    $data = array('file'=>"@{$fileLocation};filename={$fileName}");
    $headers = array(
        'X-Atlassian-Token: nocheck'
    );
 
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($curl, CURLOPT_VERBOSE, 1);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
 
    $result = curl_exec($curl);
    $ch_error = curl_error($curl);
      
    if ($ch_error) {
      
        echo "cURL Error: $ch_error";
    } else {
      
        echo $result;
    }
     
    curl_close($curl);
?>

I have PHP version 5.4.25 and following the code provided by you, but still unable to upload the attachment. See the output / result below.

For your information, I have no problem creating new issue or add comment to the issue via the REST API.

*****

The requested URL could not be retrieved

Invalid Request error was encountered while trying to process the request:

POST /jira/rest/api/latest/issue/ZZCP-27/attachments HTTP/1.1
Authorization: Basic xxxxxxxxxx
Host: jira.xxxxxxxxxx.com
Accept: */*
X-Atlassian-Token: nocheck
Content-Length: 781032
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------3405abbf3503

Some possible problems are:

Missing or unknown request method.

Missing URL.

Missing HTTP Identifier (HTTP/1.0).

Request is too large.

Content-Length missing for POST or PUT requests.

Illegal character in hostname; underscores are not allowed.

HTTP/1.1 "Expect:" feature is being asked from an HTTP/1.0 software.

Your cache administrator is root.

Hey Nicholas, Could you upload your code.

rename($_FILES["file"]["tmp_name"], $_FILES["file"]["name"]);
$attachment = $_FILES["file"]["name"];
$attachment_type =$_FILES["file"]["type"];
$attachment_size= ($_FILES["file"]["size"] / 1024);
$attachment_name = $_FILES["file"]["name"];
$json = `curl --silent -D- -u $username:$password -X POST -H "X-Atlassian-Token: nocheck" -F "file=@$attachment" http://oranization.com/jira/rest/api/2/issue/$ticket_ID/attachments`;<br< a="">>

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you