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

This widget could not be displayed.

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
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Published 12 hours ago in Marketplace Apps

The 7 hacks of highly successful automation

...there's anything I've learnt from working, it's that people are lazy! No offense to anyone reading this, but it's true and we can all admit it. The easier you make something for someone, the more...

60 views 0 8
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