Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How do I upload attachment to JIRA Issue via REST API?

Paul Shin May 7, 2013

I've followed the instruction and tried the curl example at https://docs.atlassian.com/jira/REST/latest/#id102824. However, I'm getting "Unauthorized (401)" error. So is the example wrong, or am I missing something? Thanks!

BTW, other REST API calls (creating and updating Issue) work fine.

4 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

3 votes
Faisal
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 7, 2013

Hi Paul,

Can you kindly try to run the following cURL command and see if it helps?

curl -D- -u admin:admin -X POST -H "X-Atlassian-Token: nocheck" -F "file=@das.txt" http://localhost:8082/jira1/rest/api/2/issue/MP-1/attachments

I have tested the cURL command, and it works on my end. However, kindly note to change the username/password, filename, and your instance URL as needed.

If the problem remains, that you still get the HTTP 401 error, then can you kindly try to add the attachment to other issue? This is to confirm that the problem is not related to Create Attachments permission, in the permission scheme associated to the project.

I hope that this will help!

1 vote
Sagar Kalburgi August 5, 2015

THIS CODE WORKS

 

public class JiraRest {

public static void main(String[] args) throws ClientProtocolException, IOException 
{
String pathname= "<Full path name of the attachment file>"; 
File fileUpload = new File(pathname);

HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost postRequest = new HttpPost("URL+Post REST API");
BASE64Encoder base=new BASE64Encoder();
String encoding = base.encode ("username:password".getBytes());
postRequest.setHeader("Authorization", "Basic " + encoding);
postRequest.setHeader("X-Atlassian-Token","nocheck");

MultipartEntityBuilder entity=MultipartEntityBuilder.create();
entity.addPart("file", new FileBody(fileUpload));
postRequest.setEntity( entity.build());
HttpResponse response = httpClient.execute(postRequest);
}
}


Required JARs:

All JARs in lib folder of httpcomponents-client-4.5-bin and sun.misc.BASE64Decoder.jar

manikandan.sabapathi December 27, 2016

This worked for me.. Thanks..

Divya Srinivasan December 24, 2018

Hi ,

 

@[deleted]

I am getting internal server 500 error,as below

 

HttpResponseProxy{HTTP/1.1 500 Internal Server Error [Date: Mon, 24 Dec 2018 14:04:46 GMT, Access-Control-Allow-Credentials: true, Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT, HEAD, Access-Control-Allow-Headers: Content-Type, Content-Length: 379, Connection: close, Content-Type: text/html; charset=iso-8859-1] ResponseEntityProxy{[Content-Type: text/html; charset=iso-8859-1,Content-Length: 379,Chunked: false]}}

 

Please help.

 

Thanks

Divya Srinivasan

Artis_Signature February 22, 2019

@Sagar Kalburgi 

I used the same code and getting response as 500 - Internal Server Error as below:

HttpResponseProxy{HTTP/1.1 500 [Server: Atlassian Proxy/1.13.6.2, Vary: Accept, Cache-Control: no-cache, no-store, no-transform, Content-Type: application/xml;charset=UTF-8, Strict-Transport-Security: max-age=315360000; includeSubDomains; preload, Date: Fri, 22 Feb 2019 10:08:20 GMT, ATL-TraceId: f5f5bc636e82f862, X-AACCOUNTID: 5c4f0a271487971712d6c762, X-AREQUESTID: b2d64877-b821-47ea-934d-02a18e5f05c9, X-XSS-Protection: 1; mode=block, Transfer-Encoding: chunked, Timing-Allow-Origin: *, X-AUSERNAME: admin, X-Content-Type-Options: nosniff, Connection: close, Set-Cookie: atlassian.xsrf.token=727a24c6-407f-417d-abc1-dd8d50087063_7fe1286fea3d08fc055dc4441b8fffb1ec736dba_lin; Path=/; Secure] ResponseEntityProxy{[Content-Type: application/xml;charset=UTF-8,Chunked: true]}}

Can you please help me out.

Thanks,

Soujanya

0 votes
David Kuhn August 28, 2013

Where does @das.txt reside

MattS
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 28, 2013

In the same directory you ran curl from

0 votes
Paul Shin May 7, 2013

The password was changed for the API user. Once I tried with the new password, it works fine. Thanks.

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events