Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

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

Add an attachment to existing Issue

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 Community Leader 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

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.

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 Community Leader 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. 

Hello @Warren @Ravi Sagar 

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 Community Leader 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
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