Json for Issue "Attachment" for particular issue

Hi,

I need to attach a file to particular issue by using REST API. Please provide Json REST API.

3 answers

1 accepted

Now i am able to attach a file to JIRA issue attachment section by changing the below code block,

var filename = "C:\\Users\\XXXX\\Desktop\\Sample.xlsx";
var file_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
var filecontent = new ByteArrayContent(System.IO.File.ReadAllBytes(filename));
var content = new MultipartContent("form-data", "AAAA");

content.Headers.Add("X-Atlassian-Token", "nocheck");
content.Headers.Add("charset", "UTF-8");

filecontent.Headers.ContentDisposition = 
    new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data") {
        Name="\"file\"",
        FileName = "Attachment.xlsx"
    };

filecontent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(file_type);
content.Add(filecontent);

You can try the simple curl command below by change the relative information:

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

For more information, you can refer to the rest api documentation: https://docs.atlassian.com/jira/REST/latest/#id167611

HI Teck,

Thank for replay. please share the Json file for this operation, below REST API JSON is ok or not

{
 "fields":
   {
    "attachments":[
      "filename": "D:\xx.txt"
      ]
    }
}

Hey Srini, you don't need the json file to attach the attachment. You can directly point to the file location in the file parameter of the request. Example:

curl -D- -u admin:admin -X POST -H "X-Atlassian-Token: nocheck" -F "file=@D:\xxx.txt" http://localhost:8080/rest/api/2/issue/TEST-1/attachment

Hi Tech,

I need to attach 10 files to JIRA issue, how can i achieve this programmatically by using REST API? i not using Curl command.

Can't help much on that as I have no deep knowledge on development though. Just an idea which you can try which is you can store the file location in array and then fire the REST request through a loop?

Hi Tech,

I have try the Curl command and i have received the below error message:

org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found

Hi Tech,

I have tried the Curl command for "On demand Jira", and i have recieived the below error message:

org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found

do you meant that was the simple curl command in terminal or in your coding? If it's the simple curl command request, can you post the full request so I can have a look and possible to test it as well?

Hi Tech,

Now the FileUploadException is solved.

I have used the below code to attach a file to "On demand JIRA",the response is 200, OK but Json return object is []

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Http;
using System.Json;
using System.Web.Script.Serialization;
using System.Net;

namespace JiraAttachements
{
    class Class1
    {
        public void AddAttachment()
        {
            System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
            client.DefaultRequestHeaders.ExpectContinue = false;
            client.Timeout = TimeSpan.FromMinutes(90);
            byte[] crdential = UTF8Encoding.UTF8.GetBytes("wwww:yyyy");
            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(crdential));
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

            var filecontent = new ByteArrayContent(System.IO.File.ReadAllBytes("C:\\Users\\xxx\\Desktop\\Keys.txt"));

            var content = new MultipartFormDataContent("AA");

            content.Headers.Add("X-Atlassian-Token", "nocheck");
            content.Headers.Add("charset", "UTF-8");

            content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data")
            {
                Name="\"file\"",
                FileName = "C:\\Users\\xxx\\Desktop\\Keys.txt"
            };

            content.Add(filecontent);

Hi Tech,

Please find remaining code block

try
            {
                client.PostAsync("https://{server name}.atlassian.net/rest/api/2/issue/TEST-1/attachments", content).ContinueWith(requesTask =>
                {
                    try
                    {
                        HttpResponseMessage response = requesTask.Result;
                        if (response.StatusCode == "OK")
                        {
                            Console.WriteLine(" Attached .");
                        }
                        else
                        {
                        }
                    }
                    catch (Exception exception)
                    {
                    }
                });


            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.StackTrace.ToString());
                Console.ReadLine();
            }

            Console.ReadKey();
        }



}

Pleae highlight my mistakes

Hi Guys

Anybody Worked with Ab Initio to Implement JIRA Integration

 

If yes please help me to add attachment to JIRA  with the help of Ab Initio..

 

I have been working with Jira API and have seen inconsistent results for my requests over and over again. Sometimes it works and sometimes it doesn't. Last week I was able to post attachments to issues just fine, but now an old problem occurred: the names of the attachments contain the whole path of the posted file, hence the attachments can't be opened. I use json representation to post files:

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

json_encode($array);

...

This gets the file posted but the problem is when it's posted the file names in JIRA are like this:

> /var/www/user/text.text

needless to say it can't be opened in JIRA. I had this problem before, then it suddenly disappeared, now it occurred again. I don't really get it. By the way I am not using curl for this request even though it might be recommended in the documentation.

I would appreciate it if you could help me

Best regards,

Thao

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

493 views 1 18
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you