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?
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.