Add attachment using an attachment from an other issue


#1

Hello,

I’m trying to attach a file to a cloned ticket, The original ticket have attachments that I would like to add to the new one. I’m using java and json. I already check there :

https://developer.atlassian.com/cloud/jira/platform/rest/v3/#api-api-3-issue-issueIdOrKey-attachments-post

I already have many request using jira rest api, however I’m totally stuck with this one. Here is the code I use :

HttpURLConnection connection = null;
JSONArray newJsonArrayIssue = null;
if (this.currentStatusLabel.equals(“Closed”)) {
this.openTicket(key);
}
try {

newJsonArrayIssue = new JSONArray();
JSONObject add = new JSONObject();

newJsonArrayIssue.put(this.newJsonObject.getJSONObject(“fields”).getJSONArray(“attachment”).get(0));
System.out.println(newJsonArrayIssue.toString(2));

// Construct the REST API URL
URL jiraURL = new URL(this.hubModel.getUrl() + “/rest/api/3/issue/” + this.newKey + “/attachments”);
URLConnection urlConnection = jiraURL.openConnection();

String login = this.hubModel.getIdUser() + “:” + this.hubModel.getPwUser();
byte[] authEncBytes = Base64.encodeBase64(login.getBytes());
String encoded = new String(authEncBytes);

byte[] dataBytes = newJsonArrayIssue.toString().getBytes(“UTF-8”);

// establish connection and request properties
connection = (HttpURLConnection) jiraURL.openConnection();
connection.setRequestMethod(“POST”);
connection.setRequestProperty(“Accept”, “/”);
connection.setRequestProperty(“Content-Type”, “application/json”);
connection.setRequestProperty(“Authorization”, "Basic " + encoded);
connection.setRequestProperty(“X-Atlassian-Token”, “no-check”);
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.write(dataBytes);

System.out.println("Connection Response: " + connection.getResponseCode() + ": " + connection.getResponseMessage());

wr.flush();
wr.close();

Reader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), “UTF-8”));
for (int c; (c = in.read()) >= 0; System.out.println(((char) c)))
;

} catch (Exception e) {
this.error = true;
System.out.println(error + " in addAttachment");
e.printStackTrace();
this.comment += " Issue occured while adding addAttachment.";
if (connection != null) {
try {
System.out.println(connection.getResponseMessage());

InputStream errorStream = connection.getErrorStream();
if (errorStream != null) {
Reader in = new BufferedReader(new InputStreamReader(errorStream));
for (int c; (c = in.read()) >= 0; System.out.print((char) c))
;
}
} catch (IOException e2) {
}
}
}

The JSON file I send is this one :

[{
“filename”: “”,
“size”: 738762,
“author”: {
“emailAddress”: “”,
“avatarUrls”: {
“48x48”: “”,
},
“displayName”: “”,
“name”: “”,
“self”: “”,
“active”:,
“timeZone”: “”,
“key”: “”
},
“created”: “”,
“self”: “”,
“id”: “”,
“mimeType”: “”,
“content”: “”
}]

Did someone already face this issue ?