Hi,
We are creating a tool to migrate Versionone data to JIRA. So for this I am consuming REST APIs mentioned in "https://developer.atlassian.com/cloud/jira/platform/rest/v3/#api-api-3-issue-issueIdOrKey-remotelink-linkId-put". I am struggling to upload an attachment to an issue. All the time I am getting Statuscode as "200" but there is no attachment to the issue in JIRA. Here in below is my code, please suggest me what I am doing wrong here.
Snippet
SendFileToServer("XYZ-315");
Snippet
private void SendFileToServer(string IssueKey) { FileInfo fi = new FileInfo(@"C:\VersionOneAttachments\some.png"); ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; byte[] fileContents = File.ReadAllBytes(fi.FullName);
//https://your-domain.atlassian.net/rest/api/2/issue/XYZ-315/attachments string urlPath = ConfigurationSettings.AppSettings.Get("JIRAURLForCreateIssue").ToString(); Uri webService = new Uri(string.Format("{0}/{1}/attachments", urlPath, IssueKey)); HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, webService); requestMessage.Headers.ExpectContinue = false; MultipartFormDataContent multiPartContent = new MultipartFormDataContent("----MyGreatBoundary"); ByteArrayContent byteArrayContent = new ByteArrayContent(fileContents); byteArrayContent.Headers.Add("Content-Type", "image/png"); byteArrayContent.Headers.Add("X-Atlassian-Token", "no-check"); multiPartContent.Add(byteArrayContent, "this is the name of the content", fi.Name); requestMessage.Content = multiPartContent; HttpClient httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Add("Authorization", ConfigurationSettings.AppSettings.Get("JiraAuthorizationKey").ToString()); httpClient.DefaultRequestHeaders.Add("X-Atlassian-Token", "no-check"); try { Task<HttpResponseMessage> httpRequest = httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseContentRead, CancellationToken.None); HttpResponseMessage httpResponse = httpRequest.Result; HttpStatusCode statusCode = httpResponse.StatusCode; HttpContent responseContent = httpResponse.Content; if (responseContent != null) { Task<String> stringContentsTask = responseContent.ReadAsStringAsync(); String stringContents = stringContentsTask.Result; } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
Any idea will be appreciated.
Thanks in advance
Basanta Padhi
I got solution!
just change
multiPartContent.Add(byteArrayContent, "this is the name of the content", fi.Name);
to
multiPartContent.Add(byteArrayContent, "file", fi.Name);
It's worked.
Thanks,
Anand Kumar
Thank you, @Basanta Padhi and @anand1093, both! Your code snippets helped get me up and running in a hurry!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Basanta Padhi,
I'm also facing this problem. Did you got solution?
Please share solution if you got.
Thanks,
Anand Kumar
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
public static void SendFileToServer(string issueKey)
{
FileInfo fi = new FileInfo(@"C:\Downloads\filename.xlsx");
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
byte[] fileContents = File.ReadAllBytes(fi.FullName);
//https://your-domain.atlassian.net/rest/api/2/issue/XYZ-315/attachments
string urlPath = "https://your-domain.atlassian.net/rest/api/2/issue/" + issueKey + "/attachments";
//Uri webService = new Uri(string.Format("{0}/{1}/attachments", urlPath, IssueKey));
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, urlPath);
requestMessage.Headers.ExpectContinue = false;
MultipartFormDataContent multiPartContent = new MultipartFormDataContent("----MyGreatBoundary");
ByteArrayContent byteArrayContent = new ByteArrayContent(fileContents);
byteArrayContent.Headers.Add("Content-Type", "image/png");
byteArrayContent.Headers.Add("X-Atlassian-Token", "no-check");
multiPartContent.Add(byteArrayContent, "file", fi.Name);
requestMessage.Content = multiPartContent;
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(Settings.JiraUserName + ":" + Settings.JiraAPIToken)));
httpClient.DefaultRequestHeaders.Add("X-Atlassian-Token", "no-check");
try
{
Task<HttpResponseMessage> httpRequest = httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseContentRead, CancellationToken.None);
HttpResponseMessage httpResponse = httpRequest.Result;
HttpStatusCode statusCode = httpResponse.StatusCode;
HttpContent responseContent = httpResponse.Content;
if (responseContent != null)
{
Task<String> stringContentsTask = responseContent.ReadAsStringAsync();
String stringContents = stringContentsTask.Result;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi, the code is getting executed. But particular excel or png is not getting uploaded in JIRA attachment. Can you please help me with this Chandan prajapati.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'm trying to add attachment through the above code and getting this response. Can anyone help me with this.
StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Timing-Allow-Origin: *
X-Arequestid: 4fd8a5843528593fd5f6f4675ebc0fe9
X-Seraph-Loginreason: AUTHENTICATED_FAILED
X-Content-Type-Options: nosniff
X-Xss-Protection: 1; mode=block
Atl-Traceid: 991dd907e1623a5c
Report-To: {"endpoints": [{"url": "https://dz8aopenkvv6s.cloudfront.net"}], "group": "endpoint-1", "include_subdomains": true, "max_age": 600}
Nel: {"failure_fraction": 0.001, "include_subdomains": true, "max_age": 600, "report_to": "endpoint-1"}
Strict-Transport-Security: max-age=63072000; includeSubDomains; preload
Transfer-Encoding: chunked
Cache-Control: no-store, no-transform, no-cache
Date: Tue, 18 Jul 2023 18:58:51 GMT
Set-Cookie: atlassian.xsrf.token=B659-IW08-H939-Y50O_58c27279e8f737f89f2c485a7701855499fe5be8_lout; path=/; SameSite=None; Secure
Server: AtlassianEdge
Content-Type: application/json; charset=UTF-8
}}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.