Post attachment to Jira using REST API in c# -
i have problem. when want post attachment jira using rest api have web exception 404 code (not found). method takes cookiecontainer authenticate user. code:
httpwebresponse response; string path = "c:\\users\\xxxx\\documents\\nowy.txt"; var boundary = string.format("----------{0:n}", guid.newguid()); request.cookiecontainer = new cookiecontainer(); foreach (cookie c in responsecookies) request.cookiecontainer.add(c); request.contenttype = string.format("multipart/form-data; boundary={0}", boundary); request.method = "post"; request.headers.add("x-atlassian-token: nocheck file=@my_file.txt"); request.headers.add("charset", "utf-8"); request.keepalive = false; var filecontent = file.readallbytes(path); request.protocolversion = httpversion.version10; using(var streamwriter = new streamwriter(request.getrequeststream())) { streamwriter.write(filecontent); } response = request.getresponse() httpwebresponse; stream s = response.getresponsestream(); i used many support, resolved problem. have idea?
edit 1
now jira returns 200 code attachment not added.. can tell me what's wrong in code?
public void addatachment(cookiecollection responsecookies) { request.cookiecontainer = new cookiecontainer(); foreach (cookie c in responsecookies) request.cookiecontainer.add(c); console.write(request.requesturi); httpwebresponse response; string fileurl = @"c:\users\xxxx\documents\nowy.txt"; var boundary = string.format("----------{0:n}", guid.newguid()); request.contenttype = string.format("multipart/form-data; boundary={0}", boundary); request.method = "post"; request.headers.add("x-atlassian-token", "nocheck"); memorystream postdatastream = new memorystream(); streamwriter postdatawriter = new streamwriter(postdatastream); postdatawriter.write("\r\n--" + boundary + "\r\n"); postdatawriter.write("content-disposition: form-data;" + "@file=\"{0}\";" + "filename=\"{1}\"" + "\r\ncontent-type: {2}\r\n\r\n", "myfile", path.getfilename(fileurl), path.getextension(fileurl)); postdatawriter.flush(); filestream filestream = new filestream(fileurl, filemode.open, fileaccess.read); byte[] buffer = new byte[1024]; int bytesread = 0; while ((bytesread = filestream.read(buffer, 0, buffer.length)) != 0) { postdatastream.write(buffer, 0, bytesread); } filestream.close(); postdatawriter.write("\r\n--" + boundary + "--\r\n"); postdatawriter.flush(); request.contentlength = postdatastream.length; using (stream s = request.getrequeststream()) { postdatastream.writeto(s); } postdatastream.close(); response = request.getresponse() httpwebresponse; streamreader responsereader = new streamreader(response.getresponsestream()); string reply = responsereader.readtoend(); console.write(reply); }
possible duplicate of how post attachment jira using rest api?.
from can see, problem in content-disposition header. rfc-1867 expects form-data name "file" , nothing else. more details, @ post on atlassian forums: https://answers.atlassian.com/questions/104822/jira-rest-api-attachment-incoherent-result.
you may take @ answer provided on duplicate post: https://stackoverflow.com/a/18489214/424059
Comments
Post a Comment