title: Post attachment to Jira using REST API in c#
body: <p>I Have a problem. When I want to POST some attachment to JIRA using REST API i have a Web Exception with 404 code (not found). My method takes CookieContainer to authenticate User. This is my code:</p>
  
  <pre><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() as HttpWebResponse;
          Stream s = response.GetResponseStream();
  </code></pre>
  
  <p>I used for many support, but any resolved my problem. So do you have some idea?</p>
  
  <p>EDIT 1</p>
  
  <p>now Jira returns 200 code but attachment was not added.. can you tell me what's wrong in my code?</p>
  
  <pre><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() as HttpWebResponse;
          StreamReader responseReader = new StreamReader(response.GetResponseStream());
          string reply = responseReader.ReadToEnd();
          Console.Write(reply);
      }
  </code></pre>