3

There's some documentation on the Jira site on how to add an attachment to an issue via a curl request here: https://confluence.atlassian.com/display/JIRAKB/How+to+attach+an+attachment+in+a+JIRA+issue+using+REST+API

This is the code that I used to successfully create an issue:

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");
  }
});

So in order to add an attachment to a ticket with the ID of 1200, I would think I would do something like this:

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"
        }
      }
    };

But have had no luck.

Edit: Getting somewhere. Here's what I've got:

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);
});

And it uploads a file called file.txt but when I look at the attachment it prints out a stack trace that starts out like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><status><status-code>500</status-code><stack-trace>java.lang.NullPointerException&#xD;
    at com.atlassian.plugins.rest.common.security.jersey.XsrfResourceFilter.mediaTypeToString(XsrfResourceFilter.java:91)&#xD;
    at com.atlassian.plugins.rest.common.security.jersey.XsrfResourceFilter.isXsrfable(XsrfResourceFilter.java:76)&#xD;
    at com.atlassian.plugins.rest.common.security.jersey.XsrfResourceFilter.filter(XsrfResourceFilter.java:54)&#xD;
    at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:277)&#xD;
    at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)&#xD;
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)&#xD;
    at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)&#xD;
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)&#xD;
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)&#xD;
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)&#xD;
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)&#xD;
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)&#xD;
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)&#xD;
Joshua Terrill
  • 1,995
  • 5
  • 21
  • 40

1 Answers1

1

You first try:

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"
        }
      }
    };

will not work because as it's not supported. Check Jira attachment documentation

So, probably you need to restructure your formData object to be

var formData = {
  file: fs.createReadStream('file.txt'),
};

No need for editing meta data if you don't need them. Also make sure you've got a valid stream of your file.

About stacktrace, I see stack frames related to XSRF despite adding "X-Atlassian-Token" header which doesn't make any sense for me.

Mouneer
  • 12,827
  • 2
  • 35
  • 45