How do I upload attachments via the REST API in C#?

Alex Scott July 17, 2018

I'm getting an error with the following code. Error: "Unable to upload attachments to server, issue has not been created.". Nothing in the error message helps troubleshoot the issue.

byte[] file = File.ReadAllBytes($@"{MyImageLocation}"); // Local path to .jpg
Issue issue = _jiraClient.CreateIssue("AB"); 
issue.Summary = "Hello World"
issue.Priority = "Medium"
issue.Description = "Testing attachments"
issue.AddAttachment("MyImage.jpg", file);
issue.Type = "Bug";
await issue.SaveChangesAsync();

My Jira client connection is working. I can create work items without attachments. I can attach to work items manually through the Jira web interface. The image is small.

1 answer

1 vote
Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 19, 2018

Hi Alex,

From looking over the JIRA REST API reference on the api/2/issue endpoint, I don't see any clear schema notes that indicate we can add attachments at this point.

From looking at some other threads on this topic in https://community.atlassian.com/t5/Questions/Create-Issue-with-attachment/qaq-p/280748 and https://community.atlassian.com/t5/Answers-Developer-Questions/Create-Issue-with-Attachment-using-REST-API/qaq-p/509160

It appears that the REST endpoint used to create issues does not have a means to add attachments.  Instead the suggestion appears to be to create the issue first, get the issuekey from that issue, and the make a second rest call to the api/2/issue/{issueIdOrKey}/attachments endpoint.

So I don't believe the problem is specific to using C#, but rather this appears to be a limitation of how Jira can add attachments.

Alex Scott July 20, 2018

Thanks! Per your suggestion, I tried attaching the image after saving and then saving again. For the second save, I don't need to 'await' so there's minimal downside to needing a second submission.

Suggest an answer

Log in or Sign up to answer