Create Issue with attachment?

Christoph Huber September 8, 2013

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?


13 answers

1 vote
Mauricio Leyzaola February 18, 2014

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
1 vote
Zul NS _Atlassian_
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 8, 2013

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

0 votes
ssraghavan November 30, 2020

I m not sure, if the above question is closed, as I still see the state as open, here is what you have to do,

https://<your_server>/rest/api/latest/issue/<jira_id>/attachments

authorization:

username:api_token

header:

Content-Type:multipart/form-data
X-Atlassian-Token: no-check

body:

use form-data

file =file path

 

it works like charm

0 votes
David Benton May 15, 2014

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="">>

0 votes
karunaB April 7, 2014

Hi Mohamad,

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

&lt;?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'=&gt;"@{$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);
?&gt;
Nicholas Ng August 27, 2014

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.

karunaB August 27, 2014

Hey Nicholas, Could you upload your code.

0 votes
Mohamad bin Subakin April 6, 2014

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

&lt;?php

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

	$url = 'http://xxx/rest/api/latest/issue/TEST-1/attachments';
	
	$data = array("file" =&gt; "@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);
	
?&gt;

0 votes
bhojraj bhatta January 23, 2014

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

0 votes
MB
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.
September 10, 2013

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 :)

0 votes
Christoph Huber September 10, 2013

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
}



Chris Turner February 11, 2014

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

Like # people like this
l v santhosh bethapudi October 30, 2014

Christoph Huber let me now how to attache file to issue

Like Frederik Hintz likes this
alfredocelso November 28, 2019

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?

0 votes
Christoph Huber September 9, 2013

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

0 votes
Christoph Huber September 9, 2013

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:

&lt;?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);
?&gt;

0 votes
Christoph Huber September 8, 2013

nobody knows :( ?

0 votes
Christoph Huber September 8, 2013

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" =&gt; "@fileURL");

 $data = array(
         'attachment' =&gt; array(
             "filename" =&gt; 'test',
             "content" =&gt; 'fileURL'
         ),

 );

 $data = array(
     'fields' =&gt; array(
         'attachment' =&gt; array(
             "filename" =&gt; 'test',
             "content" =&gt; 'fileURL'
         ),
     ),
 );
Zul NS _Atlassian_
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 8, 2013

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

Suggest an answer

Log in or Sign up to answer