It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

JAVA upload attachment to JIRA Issue via REST API by HttpURLConnection method? Edited

Hi everyone,

can help me anyone?

I need upload image or other files to JIRA by REST API, but i cant use HttpPost method, because with this method i have problem with company proxy. I tryed this:

public void AddAttachment(String DefectId) throws IOException, JSONException {

String CHARSET = "UTF-8";
BufferedReader br = null;


String path = "C:\\somefile.txt";
File file = new File(path);

URL url = new URL(myServer + "/rest/api/2/issue/" + DefectId + "/attachments");
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy", 8080));
HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);
conn.setRequestProperty("Cookie", cookieValue);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("X-Atlassian-Token", "nocheck");


OutputStream outputStream = conn.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, CHARSET), true);
try ( FileInputStream inputStream = new FileInputStream(file);) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = != -1) {
outputStream.write(buffer, 0, bytesRead);

if (200 <= conn.getResponseCode() && conn.getResponseCode() <= 299) {
br = new BufferedReader(new InputStreamReader(conn.getInputStream(),
} else {
br = new BufferedReader(new InputStreamReader(conn.getErrorStream(),

String lineOfResponse = "";
StringBuilder strBuild = new StringBuilder();

while ((lineOfResponse = br.readLine()) != null) {

String response = strBuild.toString();




 I have problem how a can id file(image or document) to output stream.

Authorization(by Cookies) i have in other methods and working fine.

2 answers

0 votes

Hey Tomas,


I think that you may implement something similar to the described on the following page:

Does it fit your needing?


Arthur Gonçalves | Atlassian Support

This is how i uploaded properly, dependencies okhttp and okio

private static void upload(File file) throws Exception{
final String address = "https://domain/rest/api/2/issue/issueId/attachments";
final OkHttpClient okHttpClient = new OkHttpClient();
final RequestBody formBody = new MultipartBody.Builder()
.addFormDataPart("file", file.getName(),
RequestBody.create(MediaType.parse("text/plain"), file))
final Request request = new Request.Builder().url(address).post(formBody)
.addHeader("X-Atlassian-Token", "no-check")
.addHeader("Authorization", "Basic api_token_from_your_account")
final Response response = okHttpClient.newCall(request).execute();
System.out.println(response.code() + " => " + response.body().string());
Sachintha I'm New Here Aug 18, 2019

okhttp3 or okhttp dependency?

File fileUpload = new File("Path to file");

/* Path to file : In windows- "C:\\Users\\Abhishek\\Downloads\\test.png"

                       In Linux - "/home/dir/test.png"

HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost postRequest = new HttpPost(URL);

/*  URL : https//{jira Base Url}/rest/api/2/issue/{issueKey}/attachments


String userpass = "username" + ":" + "Password";
String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
postRequest.setHeader("Authorization", basicAuth);
postRequest.setHeader("X-Atlassian-Token", "nocheck");
MultipartEntityBuilder entity = MultipartEntityBuilder.create();
entity.addPart("file", new FileBody(fileUpload));

HttpResponse jiraResponse = null;
try {
jiraResponse = httpClient.execute(postRequest);
} catch (ClientProtocolException f) {
} catch (IOException f) {

if (jiraResponse.getStatusLine().getStatusCode() == 200) {
System.out.println("Adding attachment to JIRA passed");
} else {
System.out.println("Adding attachment to JIRA failed");

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira

Demo Den Ep. 7: New Jira Cloud Reports

Learn how to use two new reports for next-gen projects in Jira Cloud:  Cumulative flow diagram and Sprint burndown chart. Ivan Teong, Product Manager, Jira Software, demos the Cumulative ...

245 views 1 2
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you