Add an attachment to existing Issue

Bindiya Vikram Naik May 16, 2021

As I got to know we cannot add an attachment at the time of creating issue. So I am trying to add an attachment to existing Issue.

The below code I have got from the already attached attachment of existing Issue. Do I need to give all the parameters below for POST?

My rest api code looks like this below:

POST URL: https://tataunistore.atlassian.net/rest/api/3/issue/Demo-230/attachments

"attachment" :
[
"0" :
{
"self" :
"https://tataunistore.atlassian.net/rest/api/3/attachment/548084",
"id" :
"548084",
"filename" :
"Capture error.JPG",
"author" :
{
"self" :
"https://tataunistore.atlassian.net/rest/api/3/user?accountId=6087d419c210c1006ca122b2",
"accountId" :
"6087d419c210c1006ca122b2",
"emailAddress" :
"bindiya.naik@tcs.com",
"avatarUrls" :
{
"48x48" :
"https://secure.gravatar.com/avatar/73f5ab93004a3907a708c0ebc8f463f8?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FBN-1.png",
"24x24" :
"https://secure.gravatar.com/avatar/73f5ab93004a3907a708c0ebc8f463f8?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FBN-1.png",
"16x16" :
"https://secure.gravatar.com/avatar/73f5ab93004a3907a708c0ebc8f463f8?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FBN-1.png",
"32x32" :
"https://secure.gravatar.com/avatar/73f5ab93004a3907a708c0ebc8f463f8?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FBN-1.png"
},
"displayName" :
"Bindiya Vikram Naik",
"active" :
true,
"timeZone" :
"Asia/Kolkata",
"accountType" :
"atlassian"
},
"created" :
"2021-05-14T15:54:54.850+0530",
"size" :
50038,
"mimeType" :
"image/jpeg",
"content" :
"https://tataunistore.atlassian.net/secure/attachment/548084/Capture+error.JPG",
"thumbnail" :
"https://tataunistore.atlassian.net/secure/thumbnail/548084/Capture+error.JPG"
}

1 answer

0 votes
Warren
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.
May 17, 2021

Hi @Bindiya Vikram Naik 

No you definitely don't need all those.

I implemented the code as per the answer by Jeff Caron in https://stackoverflow.com/questions/11886316/how-to-post-attachment-to-jira-using-rest-api and it works perfectly

Bindiya Vikram Naik May 17, 2021

sorry I could not understand.

What parameters I need to share when doing it thru REST api, What should I send  payload? not clear with that. or rather how do I send the file to be uploaded in payload.

Bindiya Vikram Naik May 17, 2021

I also get below error:

An unexpected error has occurred: (SyntaxError) : Failed to execute 'setRequestHeader' on 'XMLHttpRequest': 'multipart/form-data' is not a valid HTTP header field name.

Warren
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.
May 17, 2021

Hi @Bindiya Vikram Naik 

Below is my C# code (in it's simplest form, with no error checking) that I use to add an attachment to an existing issue.

It is called by passing in the issue key and the FileInfo version of the file

   AddAttachment("ABC-1234", fname);

 

public static void AddAttachment(string key, FileInfo Attachment)
{
HttpWebRequest request = null;

string fullURL = @"/rest/api/3/issue/" + key + "/attachments";

var boundary = string.Format("----------{0:N}", Guid.NewGuid());
var content = new MemoryStream();
var writer = new StreamWriter(content);

var fs = new FileStream(Attachment.FullName, FileMode.Open, FileAccess.Read);
var data = new byte[fs.Length];
fs.Read(data, 0, data.Length);
fs.Close();

writer.WriteLine("--{0}", boundary);
writer.WriteLine("Content-Disposition: form-data; name=\"file\"; filename=\"{0}\"", Attachment.Name);
writer.WriteLine("Content-Type: application/octet-stream");
writer.WriteLine();
writer.Flush();

content.Write(data, 0, data.Length);

writer.WriteLine();

writer.WriteLine("--" + boundary + "--");
writer.Flush();
content.Seek(0, SeekOrigin.Begin);

request = WebRequest.Create(fullURL) as HttpWebRequest;
request.Method = "POST";
request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
request.Accept = "application/json";
request.Headers.Add("Authorization", "Basic " + GetEncodedCredentials());
request.Headers.Add("X-Atlassian-Token", "nocheck");
request.ContentLength = content.Length;

using (Stream requestStream = request.GetRequestStream())
{
content.WriteTo(requestStream);
requestStream.Close();
}
}

 

If this (or what you're using) doesn't work for you, you'll need to show some code and details here so someone can try to understand what's going wrong for you. 

Bindiya Vikram Naik May 17, 2021

Hello @Warren @Ravi Sagar _Sparxsys_ 

I am trying it on Talent APItester extension in chrome. Kindly find the attachment for more clear picture.

POST URL: https://tataunistore.atlassian.net/rest/api/3/issue/256509/attachments

Problem statement: Now I get 200 Response code but still attachment is not getting uploaded in respective issue. Please help.

attach3.JPGattachee4.JPG

 

Attach2.JPG

Warren
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.
May 17, 2021

Hi @Bindiya Vikram Naik 

Ah okay, so this APITester that you're using is a bit like Postman in that you don't write any code? In that case, I can't really help much because I know nothing about APITester or what's going on "under the hood".

Sorry

Suggest an answer

Log in or Sign up to answer