1

I have tried to attach a file to JIRA issue attachment by using HttpClient post method - the return JIRA JSON object is []. Pleas find my code block below.

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

Please highlight my mistake in my code. I am struck with the multipart/form-data boundary value set procedure. Please give some examples using HttpClient post method for JIRA issue attachment.

mrz
  • 1,802
  • 2
  • 21
  • 32
srinivasan r
  • 63
  • 1
  • 1
  • 8
  • Possible duplicates of http://stackoverflow.com/questions/11886316/how-to-post-attachment-to-jira-using-rest-api/18489214#18489214 and http://stackoverflow.com/questions/17726564/post-attachment-to-jira-using-rest-api-in-c-sharp/18489325#18489325 – Jeff Caron Aug 28 '13 at 13:29

1 Answers1

1

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);
Kuf
  • 17,318
  • 6
  • 67
  • 91
srinivasan r
  • 63
  • 1
  • 1
  • 8