JIRA Rest API POST Attachment Issue

I am trying to upload an attachment (Untitled.png) using the JIRA rest API. I am using Unity C# but running into some issues.

I am following the tutorial for adding an attachment to an existing issue. That requires me to recreate this cURL request:

curl -D- -u admin:admin -X POST -H "X-Atlassian-Token: no-check" -F "file=@myfile.txt" http://myhost/rest/api/2/issue/TEST-123/attachments

in Unity C#. Here is what I have so far:

string authInfo = "user:password";
byte[] bytes = Encoding.UTF8.GetBytes(authInfo);
string finalAuth = Convert.ToBase64String(bytes);
WebRequest wwwRequest = WebRequest.Create("http://jira.myCompanyName.com/rest/api/2/issue/TEST-2/attachments");
wwwRequest.Method = "POST";
string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
wwwRequest.ContentType = "multipart/form-data; boundary=" + boundary;
wwwRequest.Headers["Authorization"] = "Basic " + finalAuth;
wwwRequest.Headers["X-Atlassian-Token"] = "no-check";
wwwRequest.Headers["Content-Disposition"] = "form-data; name=\"file\"; filename=\"Untitled.png\"";
byte[] buffer = File.ReadAllBytes(Application.dataPath + "/Untitled.png");
wwwRequest.ContentLength = buffer.Length;
Stream reqstr = wwwRequest.GetRequestStream();
reqstr.Write(buffer, 0, buffer.Length);
reqstr.Close();

yield return wwwRequest;
// Get the response from our request
string result = "";
WebResponse wwwResponse = null;
try
{
    wwwResponse = wwwRequest.GetResponse();
}
// Catch error in request
catch (WebException ex)
{
    if (((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.BadRequest)
    {
        Debug.Log("Error 400 detected!!");
    }
    wwwResponse = (WebResponse)ex.Response;
}
// Read back response
finally
{ 
    using (var streamReader = new StreamReader(wwwResponse.GetResponseStream()))
    {
        result = streamReader.ReadToEnd();
    }
}
Debug.Log(result);

Authentication works fine no errors are thrown, however the response only contains "[]", seemingly an empty JSON object, and no attachment actually gets uploaded to my JIRA issue. I know there must be something simple I am missing, because I can create issues, retrieve attachments, etc using the Rest API without any issues. Does anyone know what I doing wrong here?

1 answer

Maybe try this:
path = Application.dataPath + "/Untitled.png"; 

byte[] buffer = File.ReadAllBytes(@path);

So I double checked and I am already getting the bytes properly from the image. I did try your suggestion but no luck. There has got to be something incorrect or missing with my WebRequest. Here is the link to the action I am trying to perform: https://docs.atlassian.com/jira/REST/latest/#api/2/issue/{issueIdOrKey}/attachments-addAttachment

Here is the code but not in C#:

public class JiraRest {

public static void main(String[] args) throws ClientProtocolException, IOException 
{
String pathname= "<Full path name of the attachment file>"; 
File fileUpload = new File(pathname);

HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost postRequest = new HttpPost("URL+Post REST API");
BASE64Encoder base=new BASE64Encoder();
String encoding = base.encode ("username:password".getBytes());
postRequest.setHeader("Authorization", "Basic " + encoding);
postRequest.setHeader("X-Atlassian-Token","nocheck");

MultipartEntityBuilder entity=MultipartEntityBuilder.create();
entity.addPart("file", new FileBody(fileUpload));
postRequest.setEntity( entity.build());
HttpResponse response = httpClient.execute(postRequest);
}
}


Required JARs:

All JARs in lib folder of httpcomponents-client-4.5-bin and sun.misc.BASE64Decoder.jar

Unfortunately the MultipartEntityBuilder type is not available in Unity's version of C#. I tried to create my own multipart form using headers and the data stream, but I must not have formed it correctly; unfortunately I don't know what is wrong with my existing code.

Did you find a solution to this problem? I am having exactly the same issue, the attachment is not uploaded and the response contains an empty object []. No errors though...

Any help will be much appreciated!

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Monday in Jira Service Desk

Frequently Asked Questions for Jira Service Desk (AKA JSD FAQ)

Just the FAQs! Welcome to the Community, and this Focused-FAQ! Here, we've pulled together some of the most frequently asked questions associated with Jira Service Desk (JSD). We hope ...

107 views 0 9
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you