title: How to add a file attachment via NodeJS REST request
body: <p>There's some documentation on the Jira site on how to add an attachment to an issue via a curl request here: <a href="https://confluence.atlassian.com/display/JIRAKB/How+to+attach+an+attachment+in+a+JIRA+issue+using+REST+API" rel="nofollow">https://confluence.atlassian.com/display/JIRAKB/How+to+attach+an+attachment+in+a+JIRA+issue+using+REST+API</a></p>
  
  <p>This is the code that I used to successfully create an issue: </p>
  
  <pre><code>var request = require("request");
  var auth = "Basic " + new Buffer("user:password").toString("base64");
  var options = {
    uri: 'http://domain.com/rest/api/2/issue/',
    headers : {
      "Authorization" : auth
    },
    method: 'POST',
    json: {
      "fields": {
         "project": {
             "id": "10000"
         },
         "summary": summary,
         "description": description,
         "issuetype": {
             "name": "Bug"
         },
         "customfield_10003": {"value": value}
      }
    }
  };
  request(options, function (error, response, body) {
    if (!error) {
      console.log("Success");
    }
  });
  </code></pre>
  
  <p>So in order to add an attachment to a ticket with the ID of 1200, I would think I would do something like this:</p>
  
  <pre><code>var options = {
        uri: 'http://domain.com/rest/api/2/issue/1200/attachment/',
        headers : {
          "Authorization" : auth,
          "X-Atlassian-Token" : nocheck
        },
        method: 'POST',
        json: {
          "fields": {
              "file" : "filename.txt"
          }
        }
      };
  </code></pre>
  
  <p>But have had no luck.</p>
  
  <p>Edit: Getting somewhere. Here's what I've got:</p>
  
  <pre><code>var request = require('request');
  var fs = require("fs");
  var auth = "Basic " + new Buffer("user:password").toString("base64");
  var formData = {
    file: {
      value:  fs.createReadStream('file.txt'),
      options: {
        filename: 'file.txt',
        contentType: 'text/plain'
      }
    }
  };
  request.post({
    url:'http://domain.com/rest/api/2/issue/14000/attachments/', 
    headers : {
          "Authorization" : auth,
      "X-Atlassian-Token" : "nocheck"
        }, 
    formData: formData
  }, function optionalCallback(err, httpResponse, body) {
    if (err) {
      return console.error('upload failed:', err);
    }
    console.log('Upload successful!  Server responded with:', body);
  });
  </code></pre>
  
  <p>And it uploads a file called <code>file.txt</code> but when I look at the attachment it prints out a stack trace that starts out like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;&lt;status&gt;&lt;status-code&gt;500&lt;/status-code&gt;&lt;stack-trace&gt;java.lang.NullPointerException&amp;#xD;
      at com.atlassian.plugins.rest.common.security.jersey.XsrfResourceFilter.mediaTypeToString(XsrfResourceFilter.java:91)&amp;#xD;
      at com.atlassian.plugins.rest.common.security.jersey.XsrfResourceFilter.isXsrfable(XsrfResourceFilter.java:76)&amp;#xD;
      at com.atlassian.plugins.rest.common.security.jersey.XsrfResourceFilter.filter(XsrfResourceFilter.java:54)&amp;#xD;
      at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:277)&amp;#xD;
      at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)&amp;#xD;
      at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)&amp;#xD;
      at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)&amp;#xD;
      at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)&amp;#xD;
      at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)&amp;#xD;
      at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)&amp;#xD;
      at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)&amp;#xD;
      at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)&amp;#xD;
      at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)&amp;#xD;
  </code></pre>