How to add attachment for test step result via ZAPI and C# code? Edited

How add attachment for test step result via ZAPI and C# code:

I am trying to create attchment for test step result level. Getting success via postman rest client, but not with C# code.

Below code i got from zephyr cloud rest api for C#:

url--> http://docs.zfjcloud.apiary.io/#reference/attachment/create-attachment/create-attachment

using System;
using System.Net.Http;

var baseAddress = new Uri("https://prod-api.zephyr4jiracloud.com/connect/");

using (var httpClient = new HttpClient{ BaseAddress = baseAddress })
{
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("authorization", "JWT eyJhbGciOiJIUzI1NiI...");

httpClient.DefaultRequestHeaders.TryAddWithoutValidation("zapiaccesskey", "amlyYTo3YjU3OTBhN...");

using (var content = new StringContent("", System.Text.Encoding.Default, "multipart/form-data"))
{
using (var response = await httpClient.PostAsync("undefined", content))
{
string responseData = await response.Content.ReadAsStringAsync();
}
}
}

 

My actual C# code:-->

public void CreateAttachment()
{
var baseAddress = new Uri(zephyrBaseUrl);
var RELATIVE_PATH = "/public/rest/api/1.0/attachment";
var QUERY_STRING = "comment=comment&cycleId=0001494-242ac112-0001&entityId=0001494828799998-242ac112-0001&entityName=stepResult&issueId=10024&projectId=10000&versionId=-1";
var canonical_path = "POST&" + RELATIVE_PATH + "&" + QUERY_STRING;
var token = GenerateJsonWebToken(canonical_path);

try
{
using (var httpClient = new HttpClient { BaseAddress = baseAddress })
{
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("authorization", "JWT " + token);
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("zapiaccesskey", ACCESS_KEY);


using (var content = new MultipartFormDataContent())
{
var fileContent = new ByteArrayContent(System.IO.File.ReadAllBytes(@"D:\Test_Img.png"));
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "attachment",
FileName = "Test_Img.png",
};
content.Add(fileContent);

var result = httpClient.PostAsync(CONTEXT_PATH + RELATIVE_PATH + "?" + QUERY_STRING, content).Result;
Console.WriteLine(" \n" + result.Content.ReadAsStringAsync().Result);
}

}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}

 

Output Response:-

{"errorType":"ERROR","clientMessage":"We encountered some problems during processing this request. Please try again!","errorCode":104}

 

1 answer

Finally resolved with below C# code:

 

using (var content = new MultipartFormDataContent())
{
FileInfo fileInfo = new FileInfo(@"D:\Test_Img.png");
var fileContent = new StreamContent(fileInfo.OpenRead());
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "\"attachment\"",
FileName = "\"" + fileInfo.Name + "\""
};
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(MimeMapping.GetMimeMapping(fileInfo.Name));
content.Add(fileContent);
var result = httpClient.PostAsync(CONTEXT_PATH + RELATIVE_PATH + "?" + QUERY_STRING, content).Result;
Console.WriteLine(" \n" + result.Content.ReadAsStringAsync().Result);
Console.WriteLine(" \n" + result.ReasonPhrase.ToString());
}

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Marketplace Apps

Tips on how to choose the best estimation method for your planning

Planning and grooming sessions all come with their own sets of rules. Team members meet to estimate stories or other work items, all according to an agreed-upon process. And with every session comes ...

72 views 0 11
Read article

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