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

sachin shinde May 15, 2017

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

0 votes
sachin shinde May 16, 2017

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
TAGS
AUG Leaders

Atlassian Community Events