Json for Issue "Attachment" for particular issue

srinivasan radhakrishnan
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.
June 11, 2013

Hi,

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

3 answers

1 accepted

0 votes
Answer accepted
srinivasan radhakrishnan
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.
June 18, 2013

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

0 votes
Thao Truong August 25, 2013

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

0 votes
Teck-En
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 11, 2013

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

srinivasan radhakrishnan
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.
June 11, 2013

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"
      ]
    }
}

Teck-En
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 11, 2013

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

srinivasan radhakrishnan
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.
June 11, 2013

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.

Teck-En
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 11, 2013

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?

srinivasan radhakrishnan
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.
June 12, 2013

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

srinivasan radhakrishnan
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.
June 12, 2013

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

Teck-En
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 13, 2013

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?

srinivasan radhakrishnan
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.
June 13, 2013

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

srinivasan radhakrishnan
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.
June 13, 2013

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

Joel TStephen November 7, 2016

Hi Guys

Anybody Worked with Ab Initio to Implement JIRA Integration

 

Joel TStephen November 7, 2016

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

 

Suggest an answer

Log in or Sign up to answer