JIRA Rest API POST Attachment Issue

Thomas Bentley April 4, 2016

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

0 votes
Aleks Yenin (Polontech)
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 6, 2016
Maybe try this:
path = Application.dataPath + "/Untitled.png"; 

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

Thomas Bentley April 6, 2016

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

Aleks Yenin (Polontech)
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 8, 2016

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

Thomas Bentley April 13, 2016

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.

MR2001 June 14, 2016

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
TAGS
AUG Leaders

Atlassian Community Events