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?