Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.
×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?
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
You would have to create an issue, grab the issuekey then attach the attachment using /rest/api/2/issue/issuekey/attachments
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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="">>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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); ?>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey Nicholas, Could you upload your code.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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); ?>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Chistopher did you find the answer to your last problem. I have run into same one. If you have please help me.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Would have been good if you could have posted the working code..
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Christoph Huber let me now how to attache file to issue
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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); ?>
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.
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' ), ), );
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
How do you send the POST method? What is the content type you've specified?
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.