Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

I'm trying to create a method in C# to add an attachment to an issue

Alejandro Vexler
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 3, 2023

I'm trying to create a method in C# to add an attachment to an issue, and I'm getting the error `System.Exception: Error adding attachment: UnsupportedMediaType`.
This is my code.

 public async Task AddAttachmentAsync(string issueKey, string filePath)

    {

        try

        {

            var fileData = File.ReadAllBytes(filePath);

            var fileName = Path.GetFileName(filePath);

            var request = new RestRequest($"/rest/api/3/issue/{issueKey}/attachments", Method.Post);

            request.AddFile("file", fileData, fileName, "multipart/form-data");

            var response = await _client.ExecuteAsync(request);

            if (!response.IsSuccessful)

            {

                throw new Exception($"Error adding attachment: {response.StatusCode} - {response.Content}");

            }

        }

        catch (Exception ex)

        {

            throw ex;

        }

    }


0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events