It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Upload attachment using REST API from C#.NET Edited

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

2 answers

Hi @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!

Hi @Basanta Padhi,

I'm also facing this problem. Did you got solution?

Please share solution if you got.

Thanks,

Anand Kumar

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira

Demo Den Ep. 7: New Jira Cloud Reports

Learn how to use two new reports for next-gen projects in Jira Cloud:  Cumulative flow diagram and Sprint burndown chart. Ivan Teong, Product Manager, Jira Software, demos the Cumulative ...

303 views 1 3
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you